Exercise 32
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 meeting:

  • Inheritance
  • Polymorphism

Part 1

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

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

  2. Copy the files from the public class repository /exercises/ex32/ directory to your personal repo. Navigate to your personal repo copy – you should see files named Aclass.h, Bclass.h, main1.cpp, and README inside.

Part 2

View the README file and try to compile main1.cpp. You will find there is at least one compiler error in Bclass.h - just comment that line out and add an annotation as to why it did not compile. You will also find there are several lines in main1.cpp that do not compile. Again, comment them out but annotate in the file why they did not compile. Ask for help if you do not understand the problems indicated.

Part 3

Once the compiler errors are fixed, trace through main1.cpp by hand. It shows an example of how inheritance related classes interact with each other. Then run the code. Did you get the expected results? Why or why not? Discuss with students, CAs, or the instructor.

Part 4

Change just the show function declaration in Aclass.h to be virtual. Does that change the results when you run main1.cpp? Why or why not? What impact does additionally declare show to be virtual in Bclass.h have? Does it make a difference?