0

I know that it can be a stupid but I even don't know how to name this question. I'm non native English. I learn C++ from a book and there is a program which shows name and pay rate of employee (base class) and Manager (derived class) with added bool variable salaried. Here is the source code:

//base class
class Employee {
private:
    string name;
    double pay;
public:
    Employee() {
        name = "";
        pay = 0;
    }

    Employee(string empName, double payRate) {
        name = empName;
        pay = payRate;
    }

    string getName() const {
        return name;
    }

    void setName(string empName) {
        name = empName;
    }

    double getPay() const {
        return pay;
    }

    void setPay(double payRate) {
        pay = payRate;
    }

    string toString() {
        stringstream stm;
        stm << name << ": " << pay;
        return stm.str();

    }
};

//derived class
class Manager : public Employee {
private:
    bool salaried;
public:
    Manager(string name, double payRate, bool isSalaried)
        :Employee(name, payRate)
    {
        salaried = isSalaried;
    }
    bool getSalaried() {
        return salaried;
    }
};

int main()
{
    Employee emp1("Mary Smith", 15.00);
    cout << "Employee name: " << emp1.getName() << endl;
    cout << "Employee pay rate: " << emp1.getPay() << endl;
    Manager emp2("Bob Brown", 1500, true);
    cout << "Employee name: " << emp2.getName() << endl;
    cout << "Employee pay rate: " << emp2.getPay() << endl;
    cout << "Is Salaried: " << emp2.getSalaried() << endl;
    return 0;
}

Can someone explain me why this part

:Employee(name, payRate)

must be added to code to work properly?

mtb
  • 1,350
  • 16
  • 32
slayerr297
  • 11
  • 2
  • This just calls `Employee`'s constructor – DimChtz Jul 12 '16 at 11:17
  • It is a way to call Employee's parameterised constructor. The base class Default constructor is by default called but one has to call parameterized constructor explicitliy. – ani627 Jul 12 '16 at 11:21

6 Answers6

1

It calls the base class's (Employee) constructor and passes the name and payRate to it because they are members of Employee.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87
1

The part

:Employee(name, payRate)

you mention is used to call the constructor

Employee(string empName, double payRate)

of the base class Employee before executing the body of the constructor

Manager(string name, double payRate, bool isSalaried)

of the derived class Manager.

Codor
  • 17,447
  • 9
  • 29
  • 56
0
:Employee(name, payRate)

is the initialisation of the base class that class Manager inherits.

mtb
  • 1,350
  • 16
  • 32
0

In fact without this line the code should compile fine(because you have a default constructor for Employee), but the fields inherited from Employee will not get properly initialized in emp2. What this portion of the code does is to call the constructor of Employee to initialize the fields inherited from that class.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
0

What come after the colon on the constructor called initialization list and in your case it just initiate the base class with the suitable constructor

Ohad Eytan
  • 8,114
  • 1
  • 22
  • 31
0

The code can work properly even without the constructor(i.e..you can use the default constructor to accept the variables and implement two methods for filling the name and pay.)

class Employee {
private:
    string name;
    string temp_name;
    double temp_pay;
    double pay;
public:
    Employee() {
        name = temp_name;
        pay = temp_pay;
    }

    void getTempname(string tem){temp_name = tem;}
    void getTemppay(double py){ temp_pay = pay;}
};
The Apache
  • 1,076
  • 11
  • 28