In-Class Exercise
8. Practice¶
The aliens have found what they were looking for on Earth, and now is the time to start copying the selected Earthlings so they can leave our Galaxy and return to their home planet with the replicas.

Task Zero!
Before starting the exercises, download the initial code — we’ll extend this as we go!
Initial code
- Take a look at the
Animalclass! Is there anything that needs fixing?- Fix it!
- Implement the appropriate copy constructor(s) and assignment operator(s)!
- Examine the lifetime of the cow object in the
mainfunction in the given initial code! Fix the code if necessary! - Call the
listen()method of theSaucerinmain! What happens if we delete Mary the cow before the listening? - Modify the code so that the
Saucerstores copies of the abducted animals!- Try reallocating each animal using
new. - Test your code! Check the output of the
listen()function! - Any possible improvements?
- Try reallocating each animal using
- Implement the following methods:
Animal* clone() constmethod in theAnimalclass- It should return a new
Animalobject using the copy constructor.
- It should return a new
Cow* clone() constmethod in theCowclass- It should return a new
Cowobject using the copy constructor.
- It should return a new
Carp* clone() constmethod in theCarpclass- It should create a new
Carpobject using the copy constructor.
- It should create a new
- Should
clonebe virtual?
- Modify the
abduct()function. The clone method of the parameter should be called!- What will be the output of
listen()?
- What will be the output of
- Implement the destructor, copy constructor, and assignment operator for
Saucer!
Got stuck or couldn’t follow the class? Or just want to review?
Here’s one possible solution to the exercises!
In-class solution
Last update:
2025-11-27
Created: 2025-11-27
Created: 2025-11-27