< Back

Lecture 7- Inheritance


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

$ mkdir ~/cs170/labs/lab7 
$ cd ~/cs170/labs/lab7

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 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