Demonstrate an inheritance where subclass constructor passes argument information to base constructor - C++ Class

C++ examples for Class:Inheritance

Description

Demonstrate an inheritance where subclass constructor passes argument information to base constructor

Demo Code

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

class Employee{/*from ww  w.  j av  a  2 s.  c om*/
  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;
}

Result


Related Tutorials