← Back to Pointers
Question 726
Shelf Stores Its Own Copies
Shelf Stores Its Own Copies
Bookis written for you; leave it alone. Write the classShelf, which keeps COPIES of the books it is given in a dynamically allocated array of pointers: each element ofconst Book** m_bookspoints to aBookthat the shelf itself created withnew.m_booksis a pointer to an array of pointers: the array itself comes fromnew const Book*[n], and each element holds the address of oneBook. Releasing therefore means onedeleteper slot and then a singledelete[]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 line`Because the shelf owns copies, nothing the driver does to the original books afterwards can change what the shelf prints. The driver readsn, thennlines ofid 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 Emma`` the 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 — print one after the last line.
Hints4 available
Sign in to use hintsTopics:
Pointers
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)
Your Output
Run your code to see the output here...
Test Cases
Run your code to see test case results.