Level Inheritance Using C


Level Inheritance Using C++

Level inheritance is implemented in C++ by defining at least three classes. The first two classes have a parent-child relationship, and the second and third classes must also have a parent-child relationship. Each child class must pass the is a test in order to inherit from the parent class.

The following example shows level inheritance in a C++ program. Three classes are defined in this example: the Person class, the Student class, and the GradStudent class. The Person class is a base class and is the parent in the parent-child relationship with the Student class. The Student class is the derived class in this relationship. That is, the Student class inherits from the Person class.

The Person class defines two member functions within the public access specific section of the class definition. These are Write() and Display() . The Write() member function assigns information about a person that is received as arguments to the attributes of the class. The Display() member function shows values of those attributes on the screen.

Both the Student class definition and the GradStudent class definition are the same, as you saw in the Simple Inheritance Using C++ section of this chapter. However, you ll notice that the Student class inherits from the Person class and that the GradStudent class inherits from the Student class.

The Student class is both a derived class and a base class. It is a derived class in the parent-child relationship with the Person class, and it is a base class in the parent-child relationship with the GradStudent class.

Look carefully at the definitions of the Write() member function and Display() member function of the GradStudent class and you ll notice that both member functions access methods of the Person class and the Student class. This is made possible by level inheritance.

The Student class inherits members of the Person class that are defined in the public and protected access specifier sections of the Person class. This inheritance is passed along to the GradStudent class when the GradStudent class inherits from the Student class. Any member of the Person class that is accessible to the Student class is also accessible to the GradStudent class.

The main() function of this example is nearly identical to the main() function of the simple inheritance example, except the Write() member function is passed information about the person as well as about the student and the graduate student.

Here is the output of the following program:

ID: 100
First: Harry
Last: Kim
Graduation: 2008
ID: 101
First: Elizabeth
Last: Jones
Graduation: 2008
Major: Comp Sci
Undergrad school: Columbia
Undergrad graduation: 2002

 #include <iostream> 
#include <string.h>
using namespace std;
class Person
{
protected:
int m_ID;
char m_First[16], m_Last[16];
public:
Person()
{
m_ID = 0;
m_First[0] = m_Last[0] = '
 #include < iostream > 
#include <string.h>
using namespace std;
class Person
{
protected:
int m_ID;
char m_First[16], m_Last[16];
public:
Person()
{
m_ID = 0;
m_First[0] = m_Last[0] = '\0';
}
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
}
void Write( int ID, char First[], char Last[] )
{
m_ID = ID;
strcpy ( m_First, First );
strcpy( m_Last, Last );
}
};
class Student : public Person
{
protected:
int m_Graduation;
public:
virtual void Display()
{
Person::Display();
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
Person::Write( ID, First, Last );
m_Graduation = Graduation;
}
Student()
{
m_Graduation = 0;
}
};
class GradStudent : public Student
{
protected:
int m_UndergradGraduation;
char m_UndergradSchool[64];
char m_Major[64];
public:
GradStudent()
{
m_UndergradGraduation=0;
m_UndergradSchool[0] = m_Major[0] = '\0';
}
virtual void Write( int ID, char First[], char Last[], int Graduation,
char Major[], char UndergradSchool[], int UndergradGraduation )
{
Student::Write( ID, First, Last, Graduation );
strcpy( m_Major, Major );
strcpy( m_UndergradSchool, UndergradSchool );
m_UndergradGraduation = UndergradGraduation;
}
virtual void Display()
{
Student::Display();
cout << "Major: " << m_Major << endl;
cout << "Undergrad school: " << m_UndergradSchool << endl;
cout << "Undergrad graduation: " << m_UndergradGraduation << endl;
}
};
int main()
{
Student s;
GradStudent g;
s.Write( 100, "Harry", "Kim", 2008 );
g.Write( 101, "Elizabeth", "Jones", 2008, "Comp Sci", "Columbia", 2002 );
s.Display();
g.Display();
return 0;
}
';
}
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
}
void Write( int ID, char First[], char Last[] )
{
m_ID = ID;
strcpy( m_First, First );
strcpy( m_Last, Last );
}
};
class Student : public Person
{
protected:
int m_Graduation;
public:
virtual void Display()
{
Person::Display();
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
Person::Write( ID, First, Last );
m_Graduation = Graduation;
}
Student()
{
m_Graduation = 0;
}
};
class GradStudent : public Student
{
protected:
int m_UndergradGraduation;
char m_UndergradSchool[64];
char m_Major[64];
public:
GradStudent()
{
m_UndergradGraduation=0;
m_UndergradSchool[0] = m_Major[0] = '
 #include < iostream > 
#include <string.h>
using namespace std;
class Person
{
protected:
int m_ID;
char m_First[16], m_Last[16];
public:
Person()
{
m_ID = 0;
m_First[0] = m_Last[0] = '\0';
}
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
}
void Write( int ID, char First[], char Last[] )
{
m_ID = ID;
strcpy ( m_First, First );
strcpy( m_Last, Last );
}
};
class Student : public Person
{
protected:
int m_Graduation;
public:
virtual void Display()
{
Person::Display();
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
Person::Write( ID, First, Last );
m_Graduation = Graduation;
}
Student()
{
m_Graduation = 0;
}
};
class GradStudent : public Student
{
protected:
int m_UndergradGraduation;
char m_UndergradSchool[64];
char m_Major[64];
public:
GradStudent()
{
m_UndergradGraduation=0;
m_UndergradSchool[0] = m_Major[0] = '\0';
}
virtual void Write( int ID, char First[], char Last[], int Graduation,
char Major[], char UndergradSchool[], int UndergradGraduation )
{
Student::Write( ID, First, Last, Graduation );
strcpy( m_Major, Major );
strcpy( m_UndergradSchool, UndergradSchool );
m_UndergradGraduation = UndergradGraduation;
}
virtual void Display()
{
Student::Display();
cout << "Major: " << m_Major << endl;
cout << "Undergrad school: " << m_UndergradSchool << endl;
cout << "Undergrad graduation: " << m_UndergradGraduation << endl;
}
};
int main()
{
Student s;
GradStudent g;
s.Write( 100, "Harry", "Kim", 2008 );
g.Write( 101, "Elizabeth", "Jones", 2008, "Comp Sci", "Columbia", 2002 );
s.Display();
g.Display();
return 0;
}
';
}
virtual void Write( int ID, char First[], char Last[], int Graduation,
char Major[], char UndergradSchool[], int UndergradGraduation )
{
Student::Write( ID, First, Last, Graduation );
strcpy( m_Major, Major );
strcpy( m_UndergradSchool, UndergradSchool );
m_UndergradGraduation = UndergradGraduation;
}
virtual void Display()
{
Student::Display();
cout << "Major: " << m_Major << endl;
cout << "Undergrad school: " << m_UndergradSchool << endl;
cout << "Undergrad graduation: " << m_UndergradGraduation << endl;
}
};
int main()
{
Student s;
GradStudent g;
s.Write( 100, "Harry", "Kim", 2008 );
g.Write( 101, "Elizabeth", "Jones", 2008, "Comp Sci", "Columbia", 2002 );
s.Display();
g.Display();
return 0;
}



OOP Demystified
OOP Demystified
ISBN: 0072253630
EAN: 2147483647
Year: 2006
Pages: 130

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net