Inheritance in C++

What is Inheritance in C++ ?

C++ strongly supports the concept of re-usability. The C++ classes can be used again in several ways. Once a class has been written and tested, it can be adopted by another programmers. This is basically created by defining the new classes, reusing the properties of existing ones. The mechanism of deriving a new class from an old one is called ‘INHERTTENCE’.

How it Works?

This is often referred to as ‘IS-A’ relationship. The old class is called ‘BASE’ class and the new one is called ‘DERIVED’ class. The Base class is like a parent class and Derived class is like a child. Child inherits the properties from Parents, same way one class can acquire the properties of another class.

Syntax:

class derived-class-name : visibility-mode base-class-name
{
………
………
}

The colon (:) indicates that the derived class name is derived from the base-class-name. the visibility mode (access specifiers) is optional and if present, may be either private or protected or public. The default is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived.

Inheritance is the way to accept some of the necessary data of one class to another class, that satisfies the concept of re-usability of resources or data. A class can also inherit properties from more than one class or from more than one level.

Types of Inheritance in C++

  1. Single level Inheritance
  2. Multi level Inheritance
  3. Multiple Inheritance
  4. Hybrid Inheritance
  5. Hierarchical Inheritance

Example:

class xyz //base class
{
     members of xyz
};
class ABC : public xyz //public derivation
{
     members of ABC
};
class ABC : private XYZ //private derivation (by default)
{
     members of ABC
};

Here, keyword public and private are access specifiers. which defines the level of access of base class data into the derived class. Three types of access specifiers are used in inheritance to specify the access level.

List of Access Specifiers (access modifiers)

  1. Public
  2. Private
  3. Protected

Role of Access modifiers in Inheritance

In Inheritance, Base class data can be inherited by derived class using any one of the access specifiers. Based on the use of access modifiers derivation will perform differently.

access specifiers in inheritance
access specifiers in inheritance
access specifiers in inheritance
Table of access specifiers in inheritance

1. Public Derivation

class ABC : public XYZ //public derivation
{
     members of ABC;
};

Here, the base class is publicly inherited to the derived class.

  1. Public members of the base class is publicly inherited.
  2. Public members of the base class become public members of the derived class and therefore they are accessible to the objects of the derived class.
  3. Private members are not inherited and therefore, the private members of a base class will never become the members of its derived class.
  4. Protected members of the base class will become protected members for the derived class.

2. Private Derivation

class ABC : private XYZ  //private derivation
{
    members of ABC;
};

Here, the private inheritance of base class to the derived class.

  1. When a base class is privately inherited by a derived class, public members of the base class can only be accessed by the member functions of the derived class.
  2. Private members of base class are inaccessible to the objects of the derived class.
  3. Protected members of base class will become a  private members of derived class.

3. Protected Derivation

class ABC:protected XYZ   // protected derivation
{
    members of ABC;
};
  1. When a base class is protected inherited by a derived class, public members of the base class can only be accessed by the member functions of the derived class.
  2. Private members of base class are inaccessible to the objects of the derived class.
  3. If private members of base class are to be inherited to derived class then declare them as protected.
  4. Protected members of base class will remain protected for derived class.

Look at the below images, it shows the use of different access modifiers for the inheritance of data.

access specifiers in inheritance
access specifiers in inheritance

Facts :

  1. In all the cases, the private members are not inherited and therefore, the private members of a base class will never become the members of its derived class.
  2. In inheritance, some of the base class data elements and member functions are “inherited” into the derived class.
  3. Inheritance, when used to modify and extend the capability of the existing classes, becomes a very powerful tool for incremental program development.

Related posts