Name:

Sec: 07/17/2008

Programming Assignment Week #7

OO Inheritance: Member Functions and Variables


Adding members extends the definition of a class. It is not necessary to do this by changing the class directly; instead, you can inherit members of a class when designing another class. Inheritance lets you create classes from existing classes. The new classes are called derived classes, and the existing classes are called base classes.


Each derived class, in turn, could become a base class for a future derived class. In single inheritance, the derived class is derived from a single base class; in multiple inheritance, the derived class is derived from more than one base class.


The public members of a class become public members of the derived class. The private members of a class become private members of the derived class. However, public members of a base class can be inherited either as public members or as private members of the derived class.


Objectives

In this lab, you define derived classes from base classes.


After completing this lab, you will be able to:


Instructions

Step 1


Download the 3 files called Assg6Test.cpp, Person.cpp and Person.h from the class web site. Create a project and add these files to it in your IDE. The Person.[h/cpp] header and definition files implements a basic data type that defines a person, with 3 pieces of information the persons first and last name, and their age. The test program contains a main() function that shows how to instantiate objects of type Person, and use the accessor methods of the class.


You should get the files into a project and make sure they compile and run successfully. Once you have that working move on to step 2.

Step 2


We are going to use inheritance to extend the Person base class. We will create a new data type, called Student, which extends or inherits from the Person class. To do this you will need to add 2 new files to your project, call them Student.h and Student.cpp. As previously, the Studnt.h header file will contain the header information and declarations of the Student data type, while the Student.cpp implementation file will be where you implement the methods for your Friend class.


Your Student class should do the following:


Student s;

s.setGpa(3.5);

cout << “The GPA of s is: “ << s.getGpa() << endl;

s.setGrade(13);

cout << “s is in “ << s.getGrade() << “ grade “ << endl;



Student s;

s.setClass(“CSci 152”, 0);

s.setClass(“Hist 101”, 1);




// firstName, lastName, age, gpa, grade (

// yes I am in an equivalent of the 23rd grade :-(

Student s(“Derek”, “Harter”, 35, 3.7, 23);


Try and make sure that you chain your constructor appropriately so that you are only assigning the values of the gpa and grade in your constructor, and are using the Person base class constructor to assign the firstName, lastName and age values.



Step 3


Add appropriate test code to the main() function to test our your new Student class. Here is just a small example of what you should be able to do with your student, but you should add more to this:



Student jane; // do have an appropriate default constructor for your Student data type?

Student nate(“Nathan”, “Allen”, 22, 3.2, 16);

Student sue(“Susan”, “Monday”, 19, 2.8, 14);


cout << “sue full name: “ << sue.getFullName() << endl;

cout << “jane gpa: “ << jane.getGpa() << endl;

nate.setGrade(15);

cout << “nate grade: “ << nate.getGrade() << endl;

nate.setClass(“Astronomy 444”, 0);

nate.etClass(“Physics 389”, 1);

cout << “Nate classes: “ << nate.getClasses() << endl;


cout << “The full info for the student“ << endl;

cout << jane.str() << endl;

cout << sue.str() << endl;

cout << nate.str() << endl;


Assignment 6 Finished


You have now completed Assignment 6. If your program compiles and runs correctly and you have successfully uploaded your source file to the eCollege online submission site, then you are done.