← Back to Pointers
Question 731
HoldQueue Copies Share the Books, Moves Empty the Source
HoldQueue Copies Share the Books, Moves Empty the Source
Bookis written for you; leave it alone. Write the classHoldQueue, which keeps the ADDRESSES of books in aconst Book**array it allocated. The queue owns the array but NOT the books: copying a queue must produce a new array holding the same addresses (both queues then point at the same Book objects), moving a queue hands the array over and leaves the source empty, and no destructor may everdeletea book.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. Only the array was allocated by this class. Write every loop inside the special member that needs it; do not add private helper functions. Private members:const Book** m_books;andsize_t m_count;Public interface used by the hidden driver: ``HoldQueue(); // nullptr and 0 HoldQueue(const HoldQueue& other); // new array, same addresses HoldQueue& operator=(const HoldQueue& other); // guard self-assignment, release this array, new array with the same addresses HoldQueue(HoldQueue&& other) noexcept; // take other's array and count; leave other with nullptr and 0 HoldQueue& operator=(HoldQueue&& other) noexcept; ~HoldQueue(); // the array only HoldQueue& operator+=(const Book& book); // append the address of book (the driver never adds one twice) void print() const; // "Holds (N)" then every queued book on its own line`The driver queues two books, copy-constructs a second queue, changes a book in the driver and prints both queues (both show the change). It then move-constructs a third queue from the first and prints the first and the third, copy-assigns the second into a queue that already holds a different book, move-assigns the third into that same queue, assigns it to itself through a reference, and prints after each step. 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:
first: Holds (2) #101 The Hobbit x0 #102 Dune x1 copy: Holds (2) #101 The Hobbit x0 #102 Dune x1 first after move: Holds (0) moved: Holds (2) #101 The Hobbit x0 #102 Dune x1 target after copy-assign: Holds (2) #101 The Hobbit x0 #102 Dune x1 moved after move-assign: Holds (0) target: Holds (2) #101 The Hobbit x0 #102 Dune x1
= 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.