#include <string> #include <iostream> using namespace std; class Person { protec
ID: 645255 • Letter: #
Question
#include <string>
#include <iostream>
using namespace std;
class Person
{
protected:
string firstName;
string lastName;
public:
Person(void)
{
cout<<"In Person's constructor -- A Derived Class"<<endl;
cout<<"Enter firstname: ";
cin>>firstName;
cout<<"Enter lastname: ";
cin>>lastName;
}
string getFirstName(void)
{
return firstName;
}
string getLastName(void)
{
return lastName;
}
virtual void CalculateAndPrintPayrollInformation(void)
{
cout<<"This is Payroll Information for a Base Object"<<endl;
cout<<"Gross Pay is 0"<<endl;
cout<<"Income Tax is 0"<<endl;
cout<<"Net Pay is 0"<<endl<<endl;
}
};
class Employee : public Person
{
protected:
float annual_salary;
double gross_pay, hours_worked, hourly_rate, income_tax, net_pay;
public:
Employee()
{
cout<<"In Employee's constructor -- A Derived Class"<<endl;
cout<<"Enter Annual Salary: ";
cin>>annual_salary;
}
float getSalary(void)
{
return annual_salary;
}
void CalculateAndPrintPayrollInformation(void)
{
cout<<"In Taxpayer's constructor -- A derived class of employe"<<endl;
cout<<"Enter hours worked: ";
cin>>hours_worked;
cout<<"Enter hourly rate: ";
cin>>hourly_rate;
gross_pay = hours_worked * hourly_rate;
income_tax = gross_pay * 0.25;
net_pay = gross_pay - income_tax;
cout<<"Gross Pay = "<<gross_pay<<endl;
cout<<"Income Tax = "<<income_tax<<endl;
cout<<"Net Pay = "<<net_pay<<endl;
}
};
int main (void)
{
Person John;
Employee Mary;
cout<<endl<<endl<<endl;
cout<<"Enter John's information ";
cout<<endl<<John.getFirstName()<<" "<<John.getLastName()<<endl;
John.CalculateAndPrintPayrollInformation();
cout<<endl<<endl<<endl;
cout<<"Enter Mary's information ";
cout<<endl<<Mary.getFirstName()<<" "<<Mary.getLastName()<<" salary is "
<< Mary.getSalary()<<endl;
Mary.CalculateAndPrintPayrollInformation();
system("PAUSE");
return 0;
}
Question 5: What are the code differences between the programs presented in Step 1 and Step 2? (Hint: it
Explanation / Answer
If you want to execute the member function of derived class then, you can declare display( ) in the base class virtual which makes that function existing in appearance only but, you can't call that function. In order to make a function virtual, you have to add keyword virtual in front of a function
CalculateAndPrintPayrollInformation() function in the base class is a virtual function so we can not call it in main(). we can only call the CalculateAndPrintPayrollInformation() function of the derived class
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.