← Back to Pointers
Question 729
HoldQueue += Stores Each Address Once
HoldQueue += Stores Each Address Once
Bookis written for you; leave it alone. Complete the classHoldQueue, which keeps the ADDRESSES of books that are waiting for a librarian in a dynamically allocated arrayconst Book** m_books. The queue does not own the books: they live in the driver, so the queue must neverdeleteone of them, only the array.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. 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 ofbookto 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&bookin the last slot, releases the old array and keeps the new one. It returns*thisso 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 readsn, thennlines ofid 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
Sign in to use hintsTopics:
PointersOperator Overloading
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.