Skip to content

In-Class Exercise

7. Practice

motto


Task Zero!

Before starting the exercises, download the starter code. We will be working by extending this!

Starter Code

starter.cpp


  1. Extend the Cow class 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!
  2. 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?
  3. Create a global cow object as well! Its name should be "Global"! When is this object created, and when is it destroyed?
  4. Have a pointer that points to a Cow, and assign it the address of one of the local Cow objects!
    • Print the addresses of the objects and the value of the pointer!
  5. In main, create a dynamically allocated cow! Its name should be "Dynamic"!
    • What do you observe if you don’t explicitly call the destructor?
  6. Each cow can have one bell (represented by the Bell class). Add a Bell pointer data member to the Cow class!
    • 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 Bell pointer as a parameter!
  7. 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 Cow frees it?
    • What happens if it’s freed in main? Where can that be done?
    • What do we observe if both the Cow destructor and main delete the bell? (Does it help if we set the pointer to nullptr in the Cow class?)
  8. Modify the Cow’s sound-making method! If it has a bell, append the string returned by the bell’s makeSound method to the "Moo" string!
  9. 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, bellGenerator2 and bellGenerator3 won’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;
        }
    
  10. Examine each of the solutions from task 7 using the valgrind program!
    • You can start it by clicking the three dots next to the green run button.
  11. Test bellGenerator1 in 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;
        }
    
  12. Free the dynamically allocated bell in the Cow destructor!
  13. Test the following testCow function with valgrind! 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;
        }
    
  14. Find the errors in the code using valgrind.
    • Fix the errors until none remain!
    Faulty code

    valgrind_me.cpp


Got stuck or couldn’t follow the class?! Or just want to review?
Here’s one possible solution to the exercises!

Class exercise solution

solution.cpp



Last update: 2025-11-27
Created: 2025-11-27