Experiment 4 Step 1: In this experiment you will investigate the implementation
ID: 3681520 • Letter: E
Question
Experiment 4
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
#include
using namespace std;
class Person
{
protected:
string firstName;
string lastName;
public:
Person(void)
{
cout<<"In Person's constructor -- A Derived Class"<
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"<
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<
cout<<"Retrieving Number_one's first name and last name from class Person ";
cout<<" "<
cout<<"Retrieving Number_one's salary from class Employee ";
cout<<" "<
cout<
cout<<"Retrieving Number_two's first name and last name from class Person ";
cout<<" "<
cout<<"Retrieving Number_two's salary from class Employee ";
cout<<" "<
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!)
Explanation / Answer
Question 1: What are the names of the base and derived classes?
In the given example Person is the base class, and Employee is derived from Person.
Question 2: Describe how the derived class accesses the properties (data and functions) of the base class.
The derived class inherits the properties of base class publicly. Therefore, protected and public members of the base class are accessed from the derived 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?
The constructor of the base class i.e., the constructor of Person class is called before the constructor of the derived class, i.e, the constructor of the Employee 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!)
Private members are only accessible within the class defining them.
Protected members are accessible in the class that defines them and in classes that inherit from that class.
Ofcourse public members are accessible from anywhere, with no restriction.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.