Step 2: In this experiment you will investigate the implementation of inheritanc
ID: 3681521 • Letter: S
Question
Step 2: In this experiment you will investigate the implementation of inheritance. Enter, save, compile and execute the following program in MSVS. Call the new directory “InheritanceExp2” and the program “Inheritance2.cpp”. Answer the questions below:
#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’s a new virtual function)
Question 6: Referring to the output of the program in Step 2, how is the virtual function used in the Person and Employee classes?
Question 7: Change the type of John from Person to Employee in the main function of the program in Step 2. Execute this modified program and compare the output with the output of the original
program in Step 2. Based on the 2 different outputs, do you think the original base class implementation of the virtual function is executed in this modified program? Why or why not?
Question 8: What is the purpose of the “virtual” keyword?
Explanation / Answer
Answer for Question 1:
Here Step 1 code is not present but my assumption is in
Person class method void CalculateAndPrintPayrollInformation(void) doesn't have the virtual keyword.
but Here Step 2 code is
Person class method void CalculateAndPrintPayrollInformation(void) have the virtual keyword.
Answer for Question 2:
Virtual function is used to resolve the conflict between Person and Employee classes CalculateAndPrintPayrollInformation
access.. what will happen here means when we calling the Employee class CalculateAndPrintPayrollInformation will not call
if you are not using virtual in Person class. if you use the virtual keyword will get the CalculateAndPrintPayrollInformation
function.
Answer for Question 3:
yes this program is executed after changing the john person to employee but it will access the Base class CalculateAndPrintPayrollInformation
function.
Answer for Question 4:
A virtual function is a member function that you expect to be redefined in derived classes.
When you refer to a derived class object using a pointer or a reference to the base class,
you can call a virtual function for that object and execute the derived class's version of the function.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.