inheritance - Derived Class C++ -
this question has answer here:
i know can stupid don't know how name question. i'm non native english. learn c++ book , there program shows name , pay rate of employee (base class) , manager
(derived class) added bool
variable salaried
. here 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 explain me why part
:employee(name, payrate)
must added code work properly?
the part
:employee(name, payrate)
you mention used call constructor
employee(string empname, double payrate)
of base class employee
before executing body of constructor
manager(string name, double payrate, bool issalaried)
of derived class manager
.
Comments
Post a Comment