← Back to Pointers
Question 739
Garage Rule of Five Over an Array of Vehicle Pointers
Garage Rule of Five Over an Array of Vehicle Pointers
Vehicleis written for you; leave it alone. Write the classGarage, which owns an ARRAY OF POINTERS to vehicles it created itself:Vehicle** m_vehiclespoints at an array fromnew Vehicle*[n], and each slot holds the address of anew Vehicle. It also hasstring m_ownerandsize_t m_count. Give it all five special members, each with its own loops written out in full. Write every loop inside the special member that needs it; do not add private helper functions. Public interface used by the hidden driver: ``Garage(const string& owner); // no vehicles yet Garage(const Garage& other); // deep copy: new array AND a new Vehicle per slot Garage& operator=(const Garage& other); // guard self-assignment, delete every vehicle and the array, then deep copy Garage(Garage&& other) noexcept; // take the owner (std::move), the array and the count; leave other with an empty owner, nullptr and 0 Garage& operator=(Garage&& other) noexcept; // guard self-move, delete every vehicle and the array, take over, empty other ~Garage(); // delete every vehicle, then the array Garage& operator+=(const Vehicle& vehicle); // append new Vehicle(vehicle): allocate one slot more, copy the pointers, release the old array void print() const; // "Unowned garage" when the owner is empty; otherwise "Garage of OWNER (N)" then two spaces and the vehicle per line`The driver reads the owner,nandnlines ofplate wheels, then one extraplate wheels. It copy-constructs a second garage and adds the extra vehicle to the copy only, prints both; move-constructs a third garage from the first and prints the first and the third; builds a garage forPatholding the extra vehicle, copy-assigns the second garage into it and prints it; move-assigns the third into it and prints the third and it; then assigns it to itself and move-assigns it to itself through a reference and prints it. For the input:`Lee 2 ABC123 4 MOTO77 2 VAN900 6`` 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:
Garage of Lee (2) ABC123 (4 wheels) MOTO77 (2 wheels) Garage of Lee (3) ABC123 (4 wheels) MOTO77 (2 wheels) VAN900 (6 wheels) Unowned garage Garage of Lee (2) ABC123 (4 wheels) MOTO77 (2 wheels) Garage of Lee (3) ABC123 (4 wheels) MOTO77 (2 wheels) VAN900 (6 wheels) Unowned garage Garage of Lee (2) ABC123 (4 wheels) MOTO77 (2 wheels) Garage of Lee (2) ABC123 (4 wheels) MOTO77 (2 wheels)
= 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.