< Back

Lecture 6- Inheritance


As usual, create a directory to hold today's activities:

$ mkdir ~/cs170/labs/lab6 
$ cd ~/cs170/labs/lab6

An Introduction to Inheritance

We now understand how classes can represent our own data types. This is incredibly neat, and even allows for the efficient reuse of code. However, there are some instance where your current knowledge of classes would cause your code to not be very reusable. For example, what if something doesn't fit exactly into the templates we've created for our objects? One mechanism to fix this problem is inheritance, which allows us to specify the relationships between classes.



Lab Activity 1
Dogs

One of the more classical examples of Inheritance is describing animals. There is a well known hierarchy of animals, based off of genus, species, phylum, etc. However, that can be a little bit complicated and overwhelming. So, we'll focus on one common type of animal: dogs.

This is a very simplistic, directed, and less that useful example of inheritance. Hopefully it give you a better understanding of the syntax and the meaning behind inheritance.

Details

  1. The file Dog.h contains a declaration for a Dog class. Save this file to your directory and study it. Notice what instance variables and methods are provided.

    In addition, you need the files Dog.cc and test_dog.cc in order to run the program. Study these files, then compile and run the program to see how it currently works.

  2. One use of inheritance is to change the behavior of specific methods. Create a a class called Pomeranian that extends Dog. You should define a .h and .cc file for this new class. Your class should Override the speak method of the Dog class by defining a method in the Pomeranian class with the same method signature. The method should print something typical of a Pomeranian.

    Modify the test program program to add statements to create an instance of the Pomeranian class and make it speak (do not remove Spot). Run the program. Both dogs should speak something different because both classes have different bodies for the speak method. Notice that you had tod define the constructor method for the Pomeranian class, instead of inheriting the one from the Dog class.

  3. In addition to replacing the behavior of a method in the parent class it is also possible to extend the behavior of a method in the parent class. Create a class called Labrador also extends Dog. Add a second parameter to the constructor method that specifies the color of the lab and stores the lab's color in an attribute. Again override the speak method with something typical of a lab. Also include something about the lab's color in the speak print statement.

    Modify your testing program by adding statements to create a Labrador. Be sure to call the constructor method that specifies the name and the color. Also add a statement to make it speak.


Lab Activity 2
College Directory

As you are probably aware, the college keeps a directory of everyone affiliated with the college accessible for anyone to search. It is located on the main page of roanoke.edu. Everyone has certain information, but there is more information stored for certain classifications of people, such as Faculty or Staff. One mechanism to make storage of this information is to create separate classes for each of the classifications of people, but inheritance will eliminate a lot of duplicated code.

Details

Create the 6 different files necessary for our 3 different classifications of people:  People , Student, and Faculty. We aren't going to worry too much about actions that these classes can take, but you will want a toString method for each one of them. Both Student and Faculty will inherit the People class. You simply need to work out what information is common between Students and Faculty; Any shared information should go into their shared parent class, People.

For the sake of this program, you should store the following information (but only for the appropriate people):

Once you have finished with the class definitions, test them by creating several Students and Faculty. Make sure you can access the appropriate attributes of each object, and that you are not storing any unnecessary attributes for each class.

Example

Faculty scotty("Scotty Smith", "1/1/1900", "chssmith@roanoke.edu", "Trexler 365-B", "MCSP Department\n221 College Lane\nSalem, Va 24153")
Student scottyStudent("Scotty Smith", "1/1/1900", "cssmith@mail.roanoke.edu", "Crawford 218", "Box 1952")
cout << scotty.toString() << endl;
//Prints:
//Scotty Smith
//DOB - 1/1/1900
//Email - chssmith@roanoke.edu
//Office - Trexler 365-B
//Mail - MCSP Department
//221 College Lane
//Salem, Va 24153

cout << scotty_student.toString() << endl;
//Prints:
//Scotty Smith
//DOB - 1/1/1900
//Email - cssmith@mail.roanoke.edu
//Dorm - Crawford 218
//Mail - Box 1952

 

Challenge

In Python, we could use a Dictionary to store the list of students. However, we aren't always going to be able to use Dictionaries to solve our problems. In C++, Dictionaries are a little hard to come by, at least right now.

For now, we can simply store the directory of people in an Array. Our Array has to have a fixed size, so assume there will be no more than 100 people in the directory. Whenever someone is looking for information for a particular person we just have to find the entry in that list.

Modify your program so you can interactively add, search and print information about people.

In-Class Notes