CS32, Fall 2017

Lab06:
Inheritance fundamentals


Goals for this lab

By the time you have completed this lab, you should be able to

Step by Step Instructions

Step 1: Choose initial pair programming roles, and create a directory for this lab

If your regular partner is more than 5 minutes late, ask the TA to pair you with someone else for this week. This lab's pilot should log in. You will (both) work in this account for the rest of the lab.

Create a ~/cs32/lab06 directory and make it your current directory:

mkdir ~/cs32/lab06
cd ~/cs32/lab06

Step 2: Using inheritance

Get simple.cpp (also at ~cs32/labs/lab06/simple.cpp), compile it and run it.

Now you will start uncommenting lines to understand how inheritance works. For example, uncomment the line labelled "A1" (line number 50 in the original file) to see another way the class Child can access the variable named age in class Parent - in this case, by using the "inherited" method named getAge(). Be sure to notice that no object is specified in the call to getAge(). Do you know which object is implicitly specified in that call? Then uncomment the "A2" and "A3" lines, both of which access the inherited age data in other ways. Go ahead and compile it now to verify it still is syntactically correct, but no sense running it until you start making the "B" labelled changes.

Uncomment the "B1" section next, then compile and run. Trace what just hqppened: a Child object was used to invoke two child methods, but those methods in turn invoked Parent methods to affect the inherited variable age. Now uncomment the "B2" section, compile and run again. Then pause and reflect on what that code does: a Child object was used to invoke a Parent method directly (C.increaseAge()), proof that the Child class inherited the public interface of class Parent. Uncomment the "B3" section next, and compile and run another time. This section demonstrates how the Child object can be used to invoke a Parent class method even when that same method is defined ("overridden") in the Child class, and it demonstrates how using the method without scope resolution (Parent::) will "hide" the Parent method that has the same name.

Uncomment the "B4" section, and try to compile again. Oops. Why? What type of object is being asked to do the work in this section, a Child or a Parent? Whereas a Child inherits all of the Parent's interface, the Parent does not have any of the Child's additional features - in this case, P does not have a printAge() method. Does it?

For now, turn the "B4" section back into a comment (you'll fix it later if you have time).

Now uncomment the "C1" area - function printSSN() in class Child - and compile the program. The error you get is self-explanatory, but ask your TA in case you don't understand the problem. You will fix this problem in the next Step.

Step 3: Protected members vs. public access methods

The Child methods that worked with the inherited age variable did not suffer from the same problem as you saw for printSSN() above, because age is declared protected in class Parent instead of private like ssn. The protected access specifier allows derived classes access, but not other classes (to other classes it means the same as private). Purists avoid this type of access, and the alternative is to provide public access methods for private variables in the base class.

We'll keep the rest of these instructions short - we believe you will figure out what to do quickly on your own.

Choose an approach: either (a) change the qualifier of the ssn variable on simple.cpp from Step 2, or (b) create and implement an access method for the ssn variable, and then use that method in the Child class's printSSN function. In either case, modify the constructors accordingly to give an initial value to the variable. Then invoke the printSSN() function from main. Explain to your TA how you did that (when you get to Step 5).

Step 4: Redefining vs. overloading

  1. Download overloading.cpp (or cp from ~cs32/labs/lab06/), and type your name(s) in the header comment.
  2. Add to this program a new member variable "months" - in class Baby only.
  3. Modify the constructor in order to initialize "months" accordingly.
  4. Then add a new overloaded version of the parent function setAge(). This function should take into account not only the years but also the additional months of a baby's age and it should print accordingly the age like this (with variable years and months of course):
    This is Baby::setAge and I am overloaded Baby::age: I am: 3 and 5 months
  5. Last add a redefined printAge() version to the Baby class in order to print something like this:
    Baby::age: I am: 3 and 5 months
    And finally, uncomment the last two statements in main to test out these features.

Step 5: Show off your work and get credit for the in-lab requirements

Get your TA's attention to inspect your work, and to record completion of your in-lab work.

Don't leave early though ... begin the after-lab work below.

Step 5a. ONLY IF YOU RAN OUT OF TIME TO HAVE THE TA INSPECT YOUR WORK

If you must complete this assignment at CSIL, then you must submit it with the turnin program - but do NOT turn it in if the TA already checked you off. You MUST have both your name and your partner's name in appropriate comments in each file to be turned in to receive credit. Type the following:

turnin lab06@cs32 simple.cpp overloading.cpp

Evaluation and Grading

Each pair of students must accomplish the following to earn full credit for this lab:


After required lab-work is done

Begin with your completed version of overloading.cpp. Then add the following line to the end of main:

B.Parent::printAge();

Now compile and run the program. Is this the output you had expected? Why does this happen?

Still got some time? How about devising a way to fix the "B4" problem that you turned back into a comment near the end of Step 2. Implement your solution with a fresh copy of simple.cpp.

You may also work on PA2, but please do not interrupt the TA with questions about it if she or he is still helping people with the lab. Thanks.


Prepared by Stratos Dimopoulos (F11 TA), and Michael Costanzo.