C++ Inheritance

  • Inheritance is the mechanism of deriving a new class from an already existing or old class.
  • The old class is known as base class or parent class or super class.
  • The new class is known as derived class or child class or sub class.
  • Derived class can use all or some of the features of base class and can also define its own members.
  • Inheritance provides the concept of reusability that means once code written in the base class can be used anytime in the number of derived classes.
  • The syntax of deriving a new class from a old class is as follows:
  • class new_class_name:access-modifier old_class_name

    {

    };
  • In the above syntax:
    • class is a keyword used to create a class.
    • new_class_name is the name of the derived class.
    • old_class_name is the name of the base class.
    • access-modifier can be private, public or protected which tells about the accessibility of members of base class in the derived class.
    • If access modifier is public then:
      • All public members of base class become public members of derived class.
      • All protected members of base class become protected members of derived class.
      • Private members of base class are never inherited.
    • If access modifier is private then:
      • All public members of base class become private members of derived class.
      • All protected members of base class become private members of derived class.
      • Private members of base class are never inherited.
    • If access modifier is protected then:
      • All public members of base class become protected members of derived class.
      • All protected members of base class becomes protected members of derived class.
      • Private members of base class are never inherited.

    Types of Inheritance

    • Single level Inheritance
    • Multilevel Inheritance
    • Multiple Inheritance
    • Hierarchical Inheritance
    • Hybrid Inheritance

    Single level Inheritance

    • In single level inheritance there is only one base class and one derived class.
    • It can be represented as follows:


    • The basic syntax of single level inheritance:
    • class base_class_name

      {

      data members and functions;

      }

      class derived_class_name: access-modifier base_class_name

      {

      data members and functions;

      }
    • Here is a simple example of single level inheritance:
    • #include<iostream.h>

      #include<conio.h>

      class base

      {

      public: void display()

      {

      cout<<"Hello users";

      }

      };

      class derived:public base

      {

      };

      void main()

      {

      derived d;

      d.display();

      }
    • In the above class we derived a new class 'derived' from an existing class base and in the main function we create an object of derived class and call the function display which was inherited from class 'base'. So the output of the above program would be as follows:
    • Hello users

    Multilevel Inheritance

    • In multilevel inheritance we have one base class and one derived class at one level. At the next level derived class becomes base class for another derived class and so on.
    • This can be represented as follows:


    • The basic syntax of Multilevel inheritance:
    • class A

      {

      };

      class B:access-modifier class class A

      {

      };

      class C:access-modifier class B

      {

      };
    • Here is a simple example of Multilevel inheritance:
    • #include<iostream.h>

      #include<conio.h>

      class first

      {

      public: void show1()

      {

      cout<<"Hello from first";

      }

      };

      class second:public first

      {

      void show2()

      {

      cout<<"Hello from second";

      }

      };

      class third:public second

      {

      void show3()

      {

      show1();

      show2();

      cout<<"Hello from third";

      }

      };

      void main()

      {

      third t;

      t.show3();

      }
    • In the above example class 'first' is a base class of class 'second' and class 'second' is a base class of class 'third'. show3() is a function of class 'third', show2() is inherited to class 'third' directly from class 'second' but show1() is inherited to class 'third' indirectly from class 'first' through class 'second'. So the output of the above program would be as follows:
    • Hello from first

      Hello from second

      Hello from third

    Multiple Inheritance

    • In a multiple inheritance a derived class can have more than one base classes that means a derived class can inherit properties from more than one base classes.
    • This can be represented as follows:


    • The basic syntax of Multiple inheritance:
    • class A

      {

      };

      class B

      {

      };

      class C:access-modifier A, access-modifier B

      {

      };
    • Here is a simple example of Multiple inheritance:
    • #include<iostream.h>

      #include<conio.h>

      class base1

      {

      public: void show1()

      {

      cout<<"Hello from base1";

      }

      };

      class base2

      {

      public: void show2()

      {

      cout<<"Hello from base2";

      }

      };

      class child:public base1,public base2

      {

      public: void show3()

      {

      show1();

      show2();

      cout<<"Hello from child";

      }

      };

      void main()

      {

      child c;

      c.show3();

      }
    • In the above example class 'base1' and 'base2' are two base classes these are inherited by a single class 'child'. show3() is a function of class 'child', show2() is inherited to class 'child' directly from class 'base2' and show1() is Inherited to class 'child' directly from class 'base1'. So the output of the above program would be as follows:
    • Hello from base1

      Hello from base2

      Hello from child

    Hierarchical Inheritance

    • In Hierarchical Inheritance multiple classes share the same base class. So in this type of inheritance more than one class inherits the properties of one common base class. These derived classes can be inherited further with more classes so they become base classes for them.
    • This can be represented as follows:


    • The basic syntax of Hierarchical inheritance:
    • class A

      {

      };

      class B:access-modifier A

      {

      };

      class C:access-modifier A

      {

      };

      class D:access-modifier A

      {

      };
    • Here is a simple example of Hierarchical inheritance:
    • #include<iostream.h>

      #include<conio.h>

      class addnumbers

      {

      protected:

      int a;

      public: void get_numbers(int x)

      {

      a=x;

      }

      };

      class twotimes: public addnumbers

      {

      public: int two_times()

      {

      return(a+a);

      }

      };

      class threetimes: public addnumbers

      {

      public: int three_times()

      {

      return(a+a+a);

      }

      };

      void main()

      {

      twotimes t1;

      t1. get_numbers(2);

      cout<<"add the number two times:"<< t1. two_times()<<endl;

      threetimes t2;

      t2. get_numbers(2);

      cout<<"add the number three times:"<< t2. three_times()<<endl;

      }
    • In the above example the two derived classes "twotimes", "threetimes" uses a single base class "addnumbers". Thus two classes are inherited from a single class.This is the hierarchical inheritance OOP's concept in C++. So the output of the above program would be as follows:
    • add the number two times:4

      add the number three times:6

    Hybrid Inheritance

    • Hybrid inheritance is a type of inheritance which involves various other types of inheritance like multiple inheritance, multilevel inheritance or hierarchical inheritance.
    • This can be represented as follows:


    • In the above figure we have hierarchical inheritance as well as multiple inheritance. This is a form of hybrid inheritance. We can also represent hybrid inheritance as follows:


    • The above figure is a form of hybrid inheritance which involves multilevel and multiple inheritance.