#include <string> #include <iostream> using namespace std; class Person { protec
ID: 645254 • Letter: #
Question
#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!)
Explanation / Answer
1 Person ----Base class
Employee -- Derived class
2. In object-oriented programming (OOP), inheritance is when an object or class is based on another object or class, using the same implementation (inheriting from a class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to ahierarchy. Inheritance was invented in 1967 for Simula.
Inheritance should not be confused with subtyping.[2][3] In some languages inheritance and subtyping agree,[a] while in others they differ; in general subtyping establishes an is-a relationship, while inheritance only reuses implementation and establishes a syntactic relationship, not necessarily a semantic relationship (inheritance does not ensure behavioral subtyping). To distinguish these concepts, subtyping is also known as interface inheritance, while inheritance as defined here is known as implementation inheritance or code inheritance.[4]
Implementation inheritance is the mechanism whereby a subclass re-uses code in a base class. By default the subclass retains all of the operations of the base class, but the subclass may override some or all operations, replacing the base-class implementation with its own.
3. First called Employee() amd from this to Person().
4.
Essentially, private members allow a class to hide its implementation details from external consumers. This allows a class to better control how it data and behavior will be expressed, and allows the consumer to be ignorant of details that are not relevant to the primary purpose of the class.
Hiding implementation details improves the maintainability of a program by preventing code external from the class from establishing dependencies on those details. This allows the implementation to change independent of external consumers - with a reduced risk of breaking existing behavior. When private implementation details become public, they cannot be changed without the possibility of breaking consumers of the class that depend on those details.
Private members also allow a class to protect it's implementation from external abuse.Typically, the state of a class has internal dependencies which define when the state is valid - and when it is not. We can consider the rules that govern the validity of state information to be invariant - meaning that the class always expects them to be true. Exposing private details, allows external code to modify this state in a way that may violate the invariants, and therefore compromise the validity (and behavior) of the class.
An additional benefit of information hiding, is that it reduces the surface area that consumers of the class have to understand in order to properly interact with the class. Simplification is generally a good thing. It allows consumers to focus on understanding the public interface, and not how the class achieves its functionality.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.