Syntax for a Class Inheritance class Derived_Class : Base_Class { Derived_Class
ID: 3603203 • Letter: S
Question
Syntax for a Class Inheritance
class Derived_Class : Base_Class
{
Derived_Class members
};
Experiment
Step 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? (This is a possible job interview question! Get it right and memorize it for life!)
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?
C++, Any SPAM ANSWERS WILL BE REPORTED; OTHERWISE, a 5 stars and much more appreciation =)
Explanation / Answer
Question 1: What are the names of the base and derived classes?
Ans)
Base class = Person
Derived class = Employee
__________________
Question 2: Describe how the derived class accesses the properties (data and functions) of the base class.
Ans) We are creating the Derived class by extending the features of base class through the concept of inheritance.So We can use Base class features in Derived class(Or child 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?
Ans)First Base class constructor will be called first .Later the Derived class constructor will be called.
__________________
Question 4: What is the purpose of making members of a class protected, as opposed to public or private? (This is a possible job interview question! Get it right and memorize it for life!)
Ans)Public class members can be accessible from any other class(Just to access we have to create an instance of that class).
Private class members can be use by only that class members in which they have been declared.They cannot be accessed by outside classes.
But if we declared the class members as protected ,only its derived classes can access its base class members.Other class could not access them.
__________________
Question 5: What are the code differences between the programs presented in Step 1 and Step 2? (Hint: it’s a new virtual function)
Ans)In step1 we are using the features of the base class in the derived class.
Means Derived class is having all the features of Base class.
But In case of step 2 we are creating an virtual function and provide different function body for that same function. That means we want to tell that different employees will have different methods in calculating their payroll. But every employee will have that function.So we created that function as virtual in the Base class
__________________
Question 6: Referring to the output of the program in Step 2, how is the virtual function used in the Person and Employee classes?
Ans)We can provide our own implementation(Our own code related to Employee) for that function in derived class.Means in calculating payroll we are following different methods for different employees.
For ex: here we re taking no of hours worked and payrate perhour in calculating an Employee pay roll.For another employee we will calculate his payroll based on his sales.So we declared payroll function as virtual in base class.
__________________
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?
Ans) When we are changing the john from type Person to type Employee.He(John) became an Employee.With the object reference we are calling the CalculateAndPrintPayrollInformation(); function.AS the reference is of type Employee it will check for the CalculateAndPrintPayrollInformation(); function in the Employee class. As we are overding the function in Employee class only that function(In Employee class) will be executed,But not the virtual function in Person.
__________________
Question 8: What is the purpose of the “virtual” keyword?
Ans) If we want to override base class function in Derived classes we will declare that base class function as virtual.By seeing that virtual keyword we can understand this base class function is overridden by the Derived classes.
__________________Thank YOu
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.