CJCoding With Joseph
15free / daySign inCreate account

Car Copies Its Own Engine

Engine and most of Car are written for you. m_engine is a simple pointer to ONE Engine object created with new; there is no array, so it is released with delete, not delete[]. A Car also has a plain string 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 new Engine holding 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 *this

The 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 as Placeholder with 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 is MODEL HP hp on 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
Topics:
Pointers
📥 Auto-Input:
Civic 158\n40 10\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.