OOPortal OOPortal


Inheritance/Polymorphism  «Prev 

Inheritance example in C++

Here is the base class student:
enum year {fresh, soph,junior, senior, grad};

class student {
public:
 student(char* nm,int id,double g,year x);
 void  print() const;
protected:
 int student_id;
 double gpa;
 year y;
 char name[30];
};

and the derived class grad_student:
enum support { ta, ra, fellowship, other };
class grad_student : public student {
public:
 grad_student
(char* nm, int id, double g, year x, support t, 
char* d, char* th);
 void  print() const;
protected:
 support  s;
 char dept[10];
 char thesis[80];
};

The student members student_id, gpa, name, y, and print() are inherited by grad_student. In the class student, these members are protected. This makes them visible to the derived class, grad_student, but otherwise treated as private.
Notice that the student constructor is not inherited, even though it is a public member of the base class.