In-Class Exercise
7. Practice¶

Task Zero!
Before starting the exercises, download the starter code. We will be working by extending this!
Starter Code
- Extend the
Cowclass with a destructor. The destructor should print to the standard output:- "[name] is destroyed", where [name] should be replaced by the name of the cow being destroyed!
- Instantiate a cow in
main! The cow’s name should be "Main"!- After creating "Main", also create another cow named "Main2"! In what order will they be created and destroyed?
- Create a global cow object as well! Its name should be "Global"! When is this object created, and when is it destroyed?
- Have a pointer that points to a
Cow, and assign it the address of one of the localCowobjects!- Print the addresses of the objects and the value of the pointer!
- In
main, create a dynamically allocated cow! Its name should be "Dynamic"!- What do you observe if you don’t explicitly call the destructor?
- Each cow can have one bell (represented by the
Bellclass). Add aBellpointer data member to theCowclass!- Initially, no cow has a bell. Initialize the bell pointer to
nullptr! - Create a getter for the data member!
- Create a setter for the data member that takes a
Bellpointer as a parameter!
- Initially, no cow has a bell. Initialize the bell pointer to
- Should the stored bell be freed in the destructor? If so, who should free it, and when?
- What happens if it’s not freed anywhere?
- What happens if only the
Cowfrees it? - What happens if it’s freed in
main? Where can that be done? - What do we observe if both the
Cowdestructor andmaindelete the bell? (Does it help if we set the pointer tonullptrin theCowclass?)
- Modify the
Cow’s sound-making method! If it has a bell, append the string returned by the bell’smakeSoundmethod to the "Moo" string! - Test the following bell-setting functions!
- Copy them into your code as global functions!
- Which solution is correct? Why?
- If you compile with
-Wall -Werror,bellGenerator2andbellGenerator3won’t even compile! Why? Try compiling without those flags!
Bell-setting functions
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
void bellGenerator1(Cow& c){ unsigned bellCounter; cin >> bellCounter; c.setBell(new Bell(bellCounter)); } Bell* bellGenerator2(){ unsigned bellCounter; cin >> bellCounter; Bell newBell(bellCounter); return &newBell; } Bell& bellGenerator3(){ unsigned bellCounter; cin >> bellCounter; Bell newBell(bellCounter); return newBell; }Testing them in main
1 2 3 4 5 6 7
int main() { Cow c("Daisy"); bellGenerator1(c); //c.setBell(bellGenerator2()); //c.setBell(&bellGenerator3()); cout << c.makeSound() << endl; } - Examine each of the solutions from task 7 using the
valgrindprogram!- You can start it by clicking the three dots next to the green run button.
- Test
bellGenerator1in the following way:1 2 3 4 5 6 7
int main() { Cow c("Daisy"); Cow c2("Not Daisy"); bellGenerator1(c); //set bell c = c2; //overwrite cout << c.makeSound() << endl; } - Free the dynamically allocated bell in the
Cowdestructor! - Test the following
testCowfunction withvalgrind! What is the cause of the error? How can you fix it?1 2 3 4 5 6 7 8 9 10
void testCow(Cow c){ cout << c.makeSound() << endl; } int main() { Cow c("Daisy"); bellGenerator1(c); testCow(c); return 0; } - Find the errors in the code using
valgrind.- Fix the errors until none remain!
Faulty code
Got stuck or couldn’t follow the class?! Or just want to review?
Here’s one possible solution to the exercises!
Class exercise solution
Last update:
2025-11-27
Created: 2025-11-27
Created: 2025-11-27