CS32, Fall 2012

Lab07:
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/lab07 directory and make it your current directory:

mkdir ~/cs32/lab07
cd ~/cs32/lab07

Step 2: Using inheritance

Get simple.cpp (also at ~cs32/labs/lab07/simple.cpp), compile it and run it. Start playing with the commented lines and 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.

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 one way in the next Step, and then you will design and implement a different way to fix it as part of your homework.

Step 3: Protected members

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, but you might as well learn about it now, and then learn to get around the problem a better way for your homework.

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

Change the qualifier of the ssn variable on simple.cpp from Step 2, and 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 versus Overloading

  1. Download overloading.cpp (or cp from ~cs32/labs/lab07/), 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 lab07@cs32 simple.cpp overloading.cpp

Evaluation and Grading

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


After lab-work is done

Optional - or to do in lab after completing the required work

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.

Homework - to be done before next lab (in 2 weeks): November 29

This week's homework includes working on inheritance.cpp (also at ~cs32/labs/lab07/), so get a copy of that file now and save it in a new directory in your account, say ~/cs32/hw7/. Also get another original copy of simple.cpp and save that in your hw7 directory too.

You will use the turnin program to submit some of the work, and that must be done before your next lab - the TA will check for it then. A written part is also required, to be turned in at your lab next week. Please do NOT print the homework in Cooper Lab.

Each student must complete the paper part of the homework, even if you do the programming parts in pairs.

Here are the hw7 instructions.


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