Shelf Move Leaves the Source Empty
hardQuestion 728Book is written for you; leave it alone. Write the class Shelf, which owns copies of books in a const Book** array (each element points to a Book the shelf created with new), and give it MOVE semantics: moving takes over the other shelf's array and leaves that shelf empty, so the two never try to delete the same books. m_books is a pointer to an array of pointers: the array itself comes from new const Book*[n], and each element holds the address of one Book. Moving copies ONE pointer, the address of that array; the books themselves stay where they are. Write every loop inside the special member that needs it; do not add private helper functions.
Private members: const Book** m_books; and size_t m_count;
Public interface used by the hidden driver:
Shelf(const Book* books[], size_t count); // store a new copy of every book
Shelf(Shelf&& other) noexcept; // take over other's array and count; leave other with nullptr and 0
Shelf& operator=(Shelf&& other) noexcept; // guard self-assignment, delete every book and the array this shelf holds, take over other's resources, empty other
~Shelf(); // delete every copy, then the array
void print() const; // "Shelf (N books)" then every book on its own line, or "Empty shelf" alone when the count is 0The driver never copies a shelf, so the copy constructor and copy assignment are not needed. It move-constructs a second shelf from the first and prints both, move-assigns the second into a shelf built with one book and prints both, then move-assigns a shelf to itself through a reference and prints it, which must not destroy anything.
For the input:
2
101 3 The Hobbit
102 1 Dunethe exact output is what is shown in Expected Output.
Free the memory you allocate — this question checks for memory leaks.
Do NOT write a main function.
Expected output
after move-construct: Empty shelf Shelf (2 books) #101 The Hobbit x3 #102 Dune x1 after move-assign: Empty shelf Shelf (2 books) #101 The Hobbit x3 #102 Dune x1 after self-move: Shelf (2 books) #101 The Hobbit x3 #102 Dune x1
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...