Experiment Step 1: In this experiment you will investigate the implementation of
ID: 3637488 • Letter: E
Question
ExperimentStep 1: In this experiment you will investigate the implementation of inheritance. Enter, save, compile and execute the following program in MSVS. Call the new directory “InheritanceExp1” and the program “Inheritance1.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;
}
};
class Employee : public Person
{
protected:
float salary;
public:
Employee()
{
cout<<"In Employee's constructor -- A Derived Class"<<endl;
cout<<"Enter Salary: ";
cin>>salary;
}
float getSalary(void)
{
return salary;
}
};
int main (void)
{
Employee Number_one; //calls Constructor of Person, then Constructor of employee
Employee Number_two; //calls Constructor of Person, then Constructor of employee
cout<<endl<<endl<<endl;
cout<<"Retrieving Number_one's first name and last name from class Person ";
cout<<" "<<Number_one.getFirstName()<<" "<< Number_one.getLastName()<<endl;
cout<<"Retrieving Number_one's salary from class Employee ";
cout<<" "<<Number_one.getSalary()<<endl;
cout<<endl<<endl<<endl;
cout<<"Retrieving Number_two's first name and last name from class Person ";
cout<<" "<<Number_two.getFirstName()<<" "<< Number_two.getLastName()<<endl;
cout<<"Retrieving Number_two's salary from class Employee ";
cout<<" "<<Number_two.getSalary()<<endl;
system("PAUSE");
return 0;
}
Question 1: What are the names of the base and derived classes?
Question 2: Describe how the derived class accesses the properties (data and functions) of the base class.
Question 3: Based on the output of the code, which constructor is called first: the constructor in the derived class or the constructor in the base class?
Question 4: What is the purpose of making members of a class protected, as opposed to public or private?
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?
Please Help, I want to Compare answers. Thank you
Explanation / Answer
Question 1: What are the names of the base and derived classes? ans: Base Class:- Person Derived Class:- Employee 2. ans: Derived class Employee access the base class data using its objects (Number_0ne and Number_two) to the function called getFirstname() and getLastname() 3. ans: First constructor in base class then in derived class is called. 4. ans: protected member are further can be used in inheritance.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.