Exercise 33
Info

This is an in-class exercise. An exercise page like this one will contain a brief description but is intended to be supplemented by discussion during our meeting time. Complete the exercise to the best of your ability in the time given. Feel free to talk with other students as you work, and do not be afraid to ask questions if you get stuck. Aim to complete as much as possible during our meeting. You are encouraged to work at home to complete what you do not get through today, and ask questions over Piazza or in office hours.

Learning Objectives

Reinforces concepts learned in today's meeting:

  • Inheritance
  • Overriding
  • Abstract class

Part 1

Pull the starter code for this exercise from the public repo by taking the following steps:

  1. Log into ugrad, then navigate to the public repo and type git pull to synchronize your local working copy with the remote repo.

  2. Copy the starter code for today from the public repo exercises/ex33 into your personal repo where desired. Then navigate to your personal repo folder for this exercise. Confirm that you can see the starter files for today’s exercise by typing ls exercises/ex33 – you should see files named Aclass.h, Bclass.h, main.cpp, and README inside.

Part 2

Add a virtual function to Aclass.h named toString(). The function should return a string representation of class A in the format

[Aclass: a = value of a, d = value of d, size = size of object]

and has to be const protected. For instance, if we have an object named oA of type A with a value of 10 and d value of 100, oA.toString() may return the following string:

[Aclass: a = 10, d = 100, size = 24]

Override the toString() function in class B to return a string representation of B in the format

[Bclass: a = value of a, b = value of b, d = value of d, size = size of object]

Add accessor (i.e., getter) functions if required. Make sure you use the override keyword as necessary to force a check for proper overriding.

Part 3

Make class A an abstract class by adding a pure virtual function named int fun() const in class A. Implement the function fun in B such that it returns the multiplication of a * b * d. Make use of the override keyword as applicable. Then, modify main.cpp accordingly by commenting out lines of code (if needed) to get it to compile and run. Check the results that get printed out.

Part 4

Make a C class in a file named Cclass.h (possibly along with Cclass.cpp) that is derived from A class. This class has a private int field named e. The class should have at least one constructor with an int parameter used to set the value of e; the values of a and d from the base class should be set to default values. The class should also have a function named sete() to be used for setting the value of e. Also, the class should provide its own implementation of the function named int fun() const that returns the multiplication of e * a * d, as well as the toString() function. The toString() function should return:

[Cclass: a = value of a, d = value of d, e = value of e, size = size of object]

You may uncomment and run the corresponding code for part 4 (as well as the #include "Cclass.h" line on the top) in main.cpp to check the results.

Reminder

Remember to add and commit to your local repo copy as your work. Push to your remote repo when finished and submit to Gradescope to check your solution. Use exit to logout from your ugrad account when finished. If you continue to work on the program after class, make sure to keep your repo updated as well!