← Back to Pointers
Question 734
Car Copies Its Own Engine
Car Copies Its Own Engine
Engineand most ofCarare written for you.m_engineis a simple pointer to ONEEngineobject created withnew; there is no array, so it is released withdelete, notdelete[]. ACaralso has a plainstring m_model. Write the copy constructor and the copy assignment operator so that every car has an engine of its own: copying must create a newEngineholding the same horsepower (new Engine(*other.m_engine)), never share the pointer. -Car(const Car& other): copy the model and create this car's own engine from the other car's engine -Car& operator=(const Car& other): do nothing on self-assignment; otherwise copy the model and either delete the engine you own and create a new one, or copy the other engine's value into the engine you already own; return*thisThe driver reads a model and a horsepower, then two tune amounts. It copy-constructs a second car and tunes the copy by the first amount, prints both cars, assigns the original into a car built asPlaceholderwith 1 hp and tunes that one by the second amount, prints the original and that car, then assigns the original to itself through a reference and prints it. Each print isMODEL HP hpon its own line. For the input: ``Civic 158 40 10`` 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:
Civic 158 hp Civic 198 hp Civic 158 hp Civic 168 hp Civic 158 hp
= 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.