Simple Inheritance Using C


Simple Inheritance Using C++

Simple inheritance is implemented using C++ by defining two classes. One class is the base class, and the other class is the derived class. Make sure that the derived class passes the is a test. That is, the derived class is a base class.

The following example illustrates how to use simple inheritance in C++. The two classes defined in this program are the Student class and the GradStudent class. The Student class is the base class, and the GradStudent class is the derived class.

The Student class definition (shown here) contains two member functions: Write() and Display() . Both of these are defined within the public access specifier section of the class, which means that they can be called from the program and from the derived class.

 class Student 
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
m_ID = ID;
strcpy( m_First, First );
strcpy( m_Last, Last );
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '
 class Student 
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
m_ID = ID;
strcpy ( m_First, First );
strcpy( m_Last, Last );
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '\0';
}
};
';
}
};

The Write() member function receives student information as arguments that are then assigned to attributes of the Student class. The Display() member function displays the values of those attributes on the screen.

The attributes of the Student class are defined in the protected access specifier section of the class definition. These attributes are the student ID, student name , and whether or not the student graduated .

The GradStudent class definition (shown here) follows the Student class definition in this example. As you ll remember from your C++ course, you specify that a derived class inherits a base class by using the colon , the access specifier (optional), and the name of the base class in the class header. In this example, the GradStudent class is shown inheriting the Student class:

 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] = '
 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;
}
};
';
}
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;
}
};

The GradStudent class contains the Write() and Display() member functions in its public access specifier section. Notice that the Write() member function receives both student information and graduate student information as argument. The student information is assigned to attributes of the Student class (using the Student class s Write() method), and graduate student information is assigned to attributes of the GradStudent class. Likewise, the Display() member function of the GradStudent class displays the values assigned to attributes of both classes.

The Write() and Display() member functions of the GradStudent class have access to attributes of the Student class because the Student class is inherited by the GradStudent class and because attributes of the Student class are contained within the protected access specifier of that class.

The GradStudent class defines three attributes that specify the year the student received an undergraduate degree, the name of the undergraduate school, and the student s major. All these attributes are defined within the private accessor specifier of the class, making them accessible only to member functions of the GradStudent class.

An instance of the GradStudent object called g and a Student object called s are declared within the main() function of the program. The instance name is then used to call the Write() member function of the GradStudent class and is passed student information. This information is then shown on the screen when the Display() member function is called.

Here s what is displayed on the screen when the following program executes:

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 Student
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
m_ID = ID;
strcpy( m_First, First );
strcpy( m_Last, Last );
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '
 #include < iostream > 
#include <string.h>
using namespace std;
class Student
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
m_ID = ID;
strcpy( m_First, First );
strcpy( m_Last, Last );
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '\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;
}
';
}
};
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 Student
{
protected:
int m_Graduation, m_ID;
char m_First[16], m_Last[16];
public:
virtual void Display()
{
cout << "ID: " << m_ID << endl;
cout << "First: " << m_First << endl;
cout << "Last: " << m_Last << endl;
cout << "Graduation: " << m_Graduation << endl;
}
void Write( int ID, char First[], char Last[], int Graduation )
{
m_ID = ID;
strcpy( m_First, First );
strcpy( m_Last, Last );
m_Graduation = Graduation;
}
Student()
{
m_ID = m_Graduation = 0;
m_First[0] = m_Last[0] = '\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

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