Inheritance

Inheritance is a form of software reusability in which a new class is created from an existing class by absorbing its attributes and behaviors and embellishing the existing class with the capabilities that the new class requires.

This will:

  1. save time in program development

  2. encourage the reuse of proven and debugged high-quality software

  3. reduce problems

When creating a new class, rather than writing completely new data members and methods, you will be able to designate that the new class is to inherit the data members and methods of a previously defined base or parent class.

The new class is referred to as a derived class.

Two types of inheritance:

  1. single inheritance - a class is derived from only one base class

  2. multiple inheritance - a derived class inherits from multiple base classes

The base class will contain data members and methods that are common to all of the derived classes.

The derived class will add data members and methods that are specific to itself. It will have the ability to replace or refine the features inherited from the base class.

Three types of single inheritance:

  1. public inheritance - the public and protected members of the base class are inherited as public and protected members of the derived class, respectively. The private members are NOT accessible by the derived class

  2. protected inheritance - the public and protected members of the base class are inherited as protected members of the derived class. The private members are NOT accessible by the derived class

  3. private inheritance - the public and protected members of the base class are inherited as private members of the derived class

To specify that a class is derived from a base class:

class  derived_class_name : inheritance_type   base_class_name

class Employee
   {
   public:
      Employee (int, string, string, int);
      int getIdNumber()  { return idNumber; }
      void display();

   protected:
      int idNumber;
      string firstName, lastName;
      int deptCode;
   };


Employee::Employee(int id, string f, string l, int dept)
  {
  idNumber = id;
  firstName = f;
  lastName = l;
  deptCode = dept;
  }

void Employee::display()
  {
  cout << idNumber << ' ' << lastName << ", " << firstName
       << ' ' << deptCode;
  }
******************************************************

class HourlyEmployee : public Employee
   {
   public:
      HourlyEmployee(int, string, string, int, double, double);
      void display();

   protected:
      double hourlyWage, hoursWorked;
   };

*****************************************************

class SalariedEmployee : public Employee
   {
   public:
      SalariedEmployee(int, string, string, int, double);
      void display();

   protected:
      double salary;
   };

The constructors

Since the base class constructor initializes only its own members, a derived class must also have its own constructor to initialize its members.

The derived class calls the base class constructor to initialize the inherited data members. The derived class constructor will initialize its own data members.

SalariedEmployee::SalariedEmployee(int id, string f, string l, int dept, double sal)
: Employee(id, f, l, dept)
  {
  salary = sal;
  }

The base class methods

Because a derived class object is a base class object, it inherits all of the data members and methods from the base class. To reference a base class method:

derived_class_object.base_class_method()


SalariedEmployee salEmp;
HourlyEmployee hourlyEmp;

cout << salEmp.getIdNumber() << endl
     << hourlyEmp.getIdNumber() << endl;

Extending Base Class Methods

A derived class can extend a base class method to make it perform additional work.

To use the base class method from within a derived class, use the base class name and scope resolution operator (::)

base_class_name::base_class_method()


void SalariedEmployee::display()
  {
  Employee::display()
  cout << '$' << salary;
  }

Replacing Base Class Methods

A derived class can replace a base class method to make it perform work that is specific to the class.

To replace a base class method, simply redefine it within the derived class.

void HourlyEmployee::display()
  {
  cout << idNumber << " $" << hourlyWage << ' ' << hoursWorked;
  }