Activity 16-2 - Some Details in using Inheritance



The protected Qualifier
You cannot access a private member variable or private member function in the definition or implementation of a derived class.  You can access them by name in a derived class by classifying them using the protected qualifier.  The members cannot be accessed in classes that are not derived class.

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.
#ifndef HOURLYEMPLOYEE_H
#define HOURLYEMPLOYEE_H

#include <string>
#include "employee.h"

using namespace std;

namespace savitchemployees
{

    class HourlyEmployee : public Employee
    {
    public:
        HourlyEmployee();
        HourlyEmployee(string new_name, string new_ssn, double new_wage_rate, double new_hours);
        void set_rate(double new_wage_rate);
        double get_rate();
        void set_hours(double hours_worked);
        double get_hours();
        void give_raise(double amount);
        void print_check();
    protected:
        double wage_rate;
        double hours;
    };

}//savitchemployees

#endif //HOURLYMPLOYEE_H

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..

    void HourlyEmployee::print_check()
    {
        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:

Employee jane_e;
HourlyEmployee sally_h;

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:

sally_h.Employee::print_check( );

Some of the Things That Are Not Inherited
In general, these three types of functions are NOT inherited:
        1) constructors (including copy constructors)
        2) private members (variables and functions)
        3) destructors
        4) the assignment operator =
        6) overloaded assignment operators and constructors

Exercise 16.2
Consider the following class and function definitions that are given:

#include <iostream>
#include <cstdlib>
using namespace std;

class PartFillArray
{
public:
    PartFilledArray(int array_size);
    PartFilledArray(const PartFilledArray& object);
    ~PartFilledArray( );
    void operator = (const PartFilledArray& right_side);
    void add_value(double new_entry);
    // other members functions
    ...
}
protected:
    double *a;
    int max_number;
    int number_used;
};

PartFilledArray::PartFilledArray(int array_size) : max_number(array_size), number_used(0)
{
    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.