C++ Class Inheritance subclass constructor passes argument information to base constructor

Description

C++ Class Inheritance subclass constructor passes argument information to base constructor

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
class Advisor {}; // define an empty class

class Employee{//  ww w . ja v  a  2s . com
  public:
    Employee(const char *pName = "no name"): name(pName), average(0.0), workHour(0){
        cout << "Constructing employee " << name << endl;
    }

    void addWork(int hours, float grade){
        cout << "Adding grade to " << name << endl;
        average = workHour * average + grade;
        workHour += hours;
        average = average / workHour;
    }

    int hours() { return workHour;}
    float salary() { return average;}

  protected:
    string name;
    double average;
    int    workHour;
};

class GraduateEmployee : public Employee
{
  public:
    GraduateEmployee(const char *pName, Advisor adv, double qG = 0.0) : Employee(pName), advisor(adv), qualifierGrade(qG)
    {
        cout << "Constructing graduate employee " << pName << endl;
    }

    double qualifier() { 
       return qualifierGrade; 
    }

  protected:
    Advisor advisor;
    double qualifierGrade;
};

int main(int nNumberofArgs, char* pszArgs[])
{
    Advisor adv;

    Employee llu("LIu");
    GraduateEmployee gs("Guice", adv, 1.5);

    llu.addWork(3, 2.5);
    gs.addWork(3, 3.0);

    cout << "Matt's qualifier grade = " << gs.qualifier() << endl;

    return 0;
}



PreviousNext

Related