In: Computer Science
21. Consider the code below: [13]
Employee class:
class Employee {
public:
Employee(string theName, float thePayRate);
protected:
string getName() const;
float getPayRate() const;
float pay(float hoursWorked) const;
private:
string name;
float payRate;
};
Definitions for some of the methods follow:
Employee::Employee(string theName, float thePayRate)
{
name = theName;
payRate = thePayRate;
}
float Employee::pay(float hoursWorked) const
{
return hoursWorked * payRate;
}
Manager Class:
#include "employee.h"
class Manager : public Employee {
public:
Manager(string theName,
float thePayRate,
bool isSalaried);
protected:
bool getSalaried() const;
float pay(float hoursWorked) const;
private:
bool salaried;
};
Some of the function definitions follow:
float Manager::pay(float hoursWorked) const
{
if (getSalaried())
return Employee::getPayRate();
/* else */
return Employee::pay(hoursWorked);
}
Answer the following questions based on the code above:
Employee emp("John Burke", 25.0);
Manager mgr("Jan Kovacs", 1200.0, true);
Employee *emp1P = &emp;
cout << "Pay: " << emplP->pay(40.0);
*emp1P = &mgr;
cout << "Pay: " << emplP->pay(40.0);
Employee emp("John Burke", 25.0);
Manager mgr("Jan Kovacs", 1200.0, true);
Employee *emp1P = &emp;
cout << "Pay: " << emplP->pay(40.0);
*emp1P = &mgr;
cout << "Pay: " << emplP->pay(40.0);
void Employee:: PrintPay(float hoursWorked)
{
cout << "Pay: " << pay(hoursWorked) << endl;
}
Which version of “pay” would be called in the following call to “PrintPay”? [2]
Manager mgr;
mgr.PrintPay(40.0);
a.Class Manager is a child class of base class Employee
(Manager class is derived publicly from the base class Employee)
b.Enumerate the private members of class Manager.
Manager class has only one private member variable, that is a boolean variable salaried. As the Manager is derived publicly, all the public members of Employee become public members of Manager, and all the protected members of Employee become protected members of Manager, private members are not inherited. And if the Manager was derived using protected mode, then all the protected members of the Employee class would have been the private members of Manager class.
c.What would be the output for the following code:
Employee emp(“John Burke”,25.0)
Manager mgr(“Jan Kovacs”,1200.0,true)
Employee *emp1P=&emp;
cout<<”Pay: ”<< emp1P->pay(40.0);
cout<<”Pay: ”<< emp1P->pay(40.0);
The above code will not compile due to many reasons. First of all, pay() is the protected member method of Employee class as well as the Manager class. A protected member can be ONLY accessed by member functions or by a friend class. Secondly, on the line 5, it will show an error that ‘no known conversion from Manager* to const Employee&’, it will work only if we remove the ‘*’ before emp1P on line 5. If we solve all these problems and then execute the code, both calls will invoke the base class method pay() only. If we want to invoke the child class method like this, we have to declare pay() as the virtual function in the super class.
A) Output:
if the pay() method is declared as virtual
Pay1000Pay1200
Pay1000 is for Employee John Burke (25.0*40) and Pay1200 is for
Manager 'Jan Kovacs' as salaried = true, so it will return
payRate.
A) Output:
if the pay() method is not declared virtual
Pay1000Pay48000
For Manager ,48000(40*1200) will be displayed
C)
Employee::Pay()
printPay() is declared in Employee class . So Pay() function of Employee class will be called from printPay()
Employee::Pay()
printPay() is declared in Employee class . So Pay() function of Employee class will be called from printPay()
Hope these answers helpful
Please kindly give your valuable upvotes please it helps me alot