CJCoding With Joseph
15free / daySign inCreate account

HoldQueue += Stores Each Address Once

Book is written for you; leave it alone. Complete the class HoldQueue, which keeps the ADDRESSES of books that are waiting for a librarian in a dynamically allocated array const Book** m_books. The queue does not own the books: they live in the driver, so the queue must never delete one of them, only the array. 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. Here the queue allocated the array but NOT the books it points at.

Public interface used by the hidden driver:
``
HoldQueue();                               // no array yet, count 0
~HoldQueue();                              // release the array ONLY
HoldQueue& operator+=(const Book& book);   // see below
void print() const;                        // "Holds (N)" then every queued book on its own line
`

operator+= adds the ADDRESS of book to the queue: if that address is already in the array it does nothing, otherwise it allocates an array one slot larger, copies the existing addresses over, stores &book in the last slot, releases the old array and keeps the new one. It returns *this so calls can be chained. Compare addresses, not contents: a second object with the same id, title and copies is a different book and must be added.

The driver reads n, then n lines of id copies title, adds every book, adds the first book AGAIN, then adds a separate copy of the first book, prints the queue, changes the first book's copies to 0 and prints the queue again (the queue shows the change, because it points at the same object).

For the input:
`
2
101 3 The Hobbit
102 1 Dune
``

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:

Holds (3)
#101 The Hobbit x3
#102 Dune x1
#101 The Hobbit x3
after update:
Holds (3)
#101 The Hobbit x0
#102 Dune x1
#101 The Hobbit x3

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
PointersOperator Overloading
📥 Auto-Input:
2\n101 3 The Hobbit\n102 1 Dune\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.