Menu

Inheritance in C++

  • Inheritance is one of the important features of the oop(object-oriented programming).
  • Inheritance is the process by which objects of one class acquire the properties of objects of another class in the hierarchy.

Advantages of Inheritance

  • It ensures reusability of code
  • It is very close to real life
  • It saves memory space
  • It saves time
  • It increases the reliability of code

Access Labels

PRIVATE PUBLIC PROTECTED
Private members of the base class are inaccessible to the derived class. Private members of the base class are inaccessible to the derived class. Private members of the base class are inaccessible to the derived class.
Protected members of the base class become private members of the derived class. Protected members of the base class become protected members of the derived class  
Public members of the base class become private members of the derived class. Public members of the base class become public members of the derived class. Public members of the base class become protected members of the derived class.

Types of inheritance

  1. Single Inheritance
  2. Multiple Inheritance
  3. Hierarchical Inheritance
  4. Multilevel Inheritance
  5. Hybrid Inheritance

Single Inheritance

  • Single Inheritance is the simplest form of Inheritance.
  • In the single Inheritance, one derived class inherits only from one base class.

Multiple Inheritance

  • In the multiple Inheritance, single inheritance may inherit from more than one base class.

Hierarchical Inheritance

  • In the hierarchical Inheritance, more than one derived class may inherit from the single base class.

Multilevel Inheritance

  • In the multilevel Inheritance, the subclass acts as a base class for other classes.

Hybrid Inheritance

  • Hybrid Inheritance is a combination of multilevel and hierarchical inheritance.

Base class and derived class

  • New classes can be built from an existing class, which means that additional features can be added to an existing class without modifying it.
  • The new class is known as the derived class which inherits the members of the existing class known as the base class.
  • Implementation of inheritance in C++

    class base class{

    };

    class derived class:access label baseclass{

    } ;

Example

     class square{
       ....
       ....
       ....
     }
     class area:public square{
       .....
       .....
       .....
     }
     class perimeter:public square
     {
       .....
       .....
       .....
     }