Important Note: If you use the protected qualifier before a member variable or member function, instead of public or private, then they will be the same as private members for all non-derived classes, except that they can be accessed by derived classes.
Now, suppose in class Employee we use protected, instead of private:
//This is the HEADER FILE hourlyemployee.h.
//This is the INTERFACE for the class HourlyEmployee.
using namespace std;
class HourlyEmployee :
HourlyEmployee(string new_name, string new_ssn, double new_wage_rate, double new_hours);
void set_rate(double new_wage_rate);
void set_hours(double hours_worked);
void give_raise(double amount);
Then definitions of some of the member functions in the derived class can be simplified. Here is a simplified version of the print_check function in the derived class HourlyEmployee..
net_pay = hours * wage_rate;
cout << "\n________________________________________________\n";
cout << "Pay to the order of " << name << endl;
cout << "The sum of " << net_pay << " Dollars\n";
cout << "________________________________________________\n";
cout << "Check Stub: NOT NEGOTIABLE\n";
cout << "Employee Number: " << ssn << endl;
cout << "Hourly Employee. \nHours worked: " << hours
<< " Rate: " << wage_rate << " Pay: " << net_pay << endl;
cout << "_________________________________________________\n";
This only works when the member variables of Employee are marked protected instead of private. As you have noticed the net_pay and ssn can be accessed by name because they were marked protected in the base class Employee.
Accessing a Redefined Base Function
Suppose you define a function so that it has a different definition in the derived class from what it had in the base class. The definition that was given in the base class is not completely lost in the derived class objects. However, to invoke the version of the function given in the base class with an object in the derived class, you should use the scope resolution operator with the name of the base class. Here is an example. In the previous Activity, you redefined function print_check in the derived class HourlyEmployee. Now, suppose you have an object of each class as in:
Then jane_e.print_check( ); uses the definition of print_check as it is given in the class Employee, and sally_h.print_check( ); uses the version of this function as it is given in the class HourlyEmployee.
Now, How would you invoke the version of print_check as it is given in the base class Employee with the derived object sally_h? Here is how you would Do this:
Some of the Things That Are Not
In general, these three types of functions are NOT inherited:
1) constructors (including copy constructors)
2) private members (variables and functions)
4) the assignment operator =
6) overloaded assignment operators and constructors
Consider the following class and function definitions that are given:
using namespace std;
PartFilledArray(const PartFilledArray& object);
void operator = (const PartFilledArray& right_side);
void add_value(double new_entry);
// other members functions
PartFilledArray::PartFilledArray(int array_size) : max_number(array_size),
a = new double[max_number];
Write the definitions for the member function add_value, the copy constructor, the overloaded assignment operator, and the destructor for this class.