Inheritance/Polymorphism  «Prev  Next»
Lesson 6 A derived class
Objective Constructors for base and derived classes

Code that references revised grad_student class

Try out some code that references a revised grad_student class and determine why the revised class causes an error. In our student/graduate student example, the base class constructor is:

student::student(char* nm, int id, double g, year x) :
  student_id(id), gpa(g), y(x){}
  strcpy(name, nm);         

The constructor for the base class does a series of simple initializations. It then calls strcpy() to copy over the student's name.
The derived class constructor is:

grad_student::grad_student (
    char* nm, int id, double g, year x, support t,
    char* d, char* th) : student(nm, id, g, x), s(t)
    {
     strcpy(dept, d);
     strcpy(thesis, th);
    }

Notice that the constructor for student is invoked as part of the initializer list. This is usual, and, logically, the base class object needs to be constructed first, before the object can be completed.

Base Derived - Exercise

Click the Exercise link below to try out some code that references a revised grad_student class and determine why the revised class does not work
Base Derived - Exercise