CJCoding With Joseph
15free / daySign inCreate account

Shelf Stores Its Own Copies

Book 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 line
`

Because 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 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
Topics:
Pointers
📥 Auto-Input:
3\n101 3 The Hobbit\n102 1 Dune\n103 2 Emma\n
This input is automatically fed to your program's stdin
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.