Shelf Stores Its Own Copies
mediumQuestion 726Book is written for you; leave it alone. Write the class Shelf, which keeps COPIES of the books it is given in a dynamically allocated array of pointers: each element of const Book** m_books points to a Book that the shelf itself created with new. 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. Releasing therefore means one delete per slot and then a single delete[] for the array.
Private members:
- const Book** m_books;
- size_t m_count;
Public interface used by the hidden driver:
Shelf(const Book* books[], size_t count); // allocate an array of count pointers and store a NEW COPY of every book in it
~Shelf(); // delete every copy, then the array itself
void print() const; // "Shelf (N books)" then every book on its own lineBecause the shelf owns copies, nothing the driver does to the original books afterwards can change what the shelf prints. The driver reads n, then n lines of id copies title (the title may contain spaces), builds the shelf from the addresses of those books, changes the first book's copies to 99, then prints the shelf and the changed original.
For the input:
3
101 3 The Hobbit
102 1 Dune
103 2 Emmathe 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
Shelf (3 books) #101 The Hobbit x3 #102 Dune x1 #103 Emma x2 Original now: #101 The Hobbit x99
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...