Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

#include #include using namespace std; class Person { protected: string firstNam

ID: 3559647 • Letter: #

Question

#include #include using namespace std; class Person { protected: string firstName; string lastName; public: Person(void) { cout << "In the Person constructor (a derived class)" << endl; cout << "Enter a first name: "; cin >> firstName; cout << "Enter a last name: "; cin >> lastName; } string getFirstName(void) { return firstName; } string getLastName(void) { return lastName; } }; class Employee : public Person { protected: float salary; public: Employee() { cout << "In the Employee constructor (a derived class)" << endl; cout << "Enter a salary (no commas): "; 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!)

Explanation / Answer

#include
#include
using namespace std;
class Person
{
protected:
string firstName;
string lastName;
public:
Person(void)
{
cout << "In the Person constructor (a derived class)" << endl;
cout << "Enter a first name: ";
cin >> firstName;
cout << "Enter a last name: ";
cin >> lastName;
}
string getFirstName(void)
{
return firstName;
}
string getLastName(void)
{
return lastName;
}
};
class Employee : public Person
{
protected: float salary;
public: Employee()
{
cout << "In the Employee constructor (a derived class)" << endl;
cout << "Enter a salary (no commas): ";
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?

base class is Person and derived class is Employee.

Question 2: Describe how the derived class accesses the properties (data and functions) of the base class.

derived class can access firstName of lastName of base class i.e Person. because those are protected.and all
public members and methods can directly accessed by 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 in the base class is called first.

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!)

only protected members can be accessed by derived class.
even public members can be accessed by derived class but it violates encapsulation.
and private members cant be accessed by derived class.
so it should be protected.