OOPortal OOPortal


Operator Overloading  «Prev 

Constructors for base and derived classes


Objective: Try out some code that references to a revised grad_student class and determine why the revised class causes an error.

Background

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

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

Instructions

Let us change the declaration of the grad_student class from:
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;
   grad_student();
   support  s;
   char     dept[10];
   char     thesis[80];
};

to:
enum support { ta, ra, fellowship, other };

class grad_student : student { 
public:
   grad_student
     (char* nm, int id, double g, year x, support t,
      char* d, char* th);
   void  print() const;
   grad_student();
   support  s;
   char     dept[10];
   char     thesis[80];
};

Explain what goes wrong when you then run the following code:
void main(){
   grad_student s;

   strcpy(s.name , "Charles Babbage");
}

Type your answer below, then click the Submit button when you are ready to submit this exercise.