inheritance and their types

If your class’s concept is clear, you can easily understand it. the following topics we will discuss here,

 

inheritance

This is an important concept of OOPs. As we know that we can reuse the code to perform the same task in c ++. The same happens in inheritance.

inheritance
re-usability

In inheritance, we add or inherit the property of one class to another class without modifying any of the classes. The class whose members inherit is called base or parent class and the one who inherits is called derive or child class.

you can understand it in this way, like in the real world, a child inherits some character or property from his parents, some of these character or property can be his own, and some inherit from parents. as the name of the child is its own and the surname receives it in heritage.

similarly in OOPs, some character or property of one class is inherited in another class, by doing so the properties already exist in the first class whereas by inheriting, those properties are also passed to another class.

A program can have more than one base class and derive classes, in such a way, a base class can be derived for someone else and that derive class is base class for someone else.

It can understand something like this, in the way that the grandfather will be the base for father, and the father will be the base for the child while the father will derive for the grandfather, and the child will derive for the father.

Let’s more about inheritance,

How to inherit a base class into derive-class ? 

SYNTEX:

class derive-class-name : visibility-mode base-class-name

The visibility-mode represents here, that the class is inherited from public , private mode or protected mode.

means, in C++ we can inherit a class in three ways. so before starting inheritance, we discuss about these ways which are called access specifier or visibility-mode.

let’s know about them,

access specifiers in inheritance

  • Private mode
  • Public mode
  • Protected mode

Private mode

When a class is inherited through private mode, all members of the base class become private to the derive class which can only be accessed by the members of derive class in which it is inherited.

That is, only members of the drive class can access them, not objects of the drive class. (as we know private members can not be accessed by object)

class derive-class-name : private base-class-name

Remember, if any type of access specifier is not present, then it will by default private mode. such as,

class derive-class-name : base-class-name

But be aware that private members of a class cannot be inherited.

Public mode

When a class inherits publicly. So only the public members of the base class become public for the derive class, which can be accessed from both the member and object of the derive class and can also be inherited in another class.

class derive-class-name : public base-class-name

But remember that private members of the class will not inherit. But if we want to access the private members of Base class, then we have to declare those members in a protected mode (here in after mentioned)

Protected mode

As we know that private members of a class cannot be inherited i.e. a member of the derive class cannot access the private member of the base class. If we have to access private members, then we will have to declare all member publicly, but doing so will affect the data hiding.

To overcome this problem, there is a third way to declare member in c ++ which is called protected mode. In this, the declared member can be accessed from the member of the derive class,

class derive-class-name : protected base-class-name

Here, in brief

class class_name
{
  private:
     private member;. // Accessible only same class member
  protected:
    protected member; // accessible derive class
  public:
    public member; //accessible own class and derive 
};

Diagram Here

Now you are ready to learn inheritance,


Type of inheritance

There are 5 type of inheritance available in C++, these are follows-

 

Single level inheritance

There is only one base class of a derive class. In this type of inheritance, we take only two classes. In which the property of the first class (base class) is inherited in the second class

SYNTEX:

class derive-class-name : base-class-name
{
  Member of derive class;
}

Example:-

In this , the first class is taking two numbers from the user and the second class is calculating these numbers and printing the total

single-level-inheritance

Here is the program,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class first     //base class
{
  protected:
  int a,b;
  public:
  void get_num(int x,int y)
  {
     a = x;
     b = y;
  }
}; // first class terminate

class second:public first   // class first inherit
{
   public:
   void get_sum()
   {
    int sum;
    sum = a+b;
    cout<<"Total: "<<sum;
   }
};  // second class terminate

void main() // main function start here
{
  int a,b;
  clrscr();

  second obj;   //class second object declare

  cout<<"Enter two number: ";
  cin>>a>>b;

  obj.get_num(a,b); // class first member call
  obj.get_sum();    // class second member call

  getch();
}
OUTPUT:-
Enter two number: 5 6
Total: 11

 

Multi-level inheritance

In this type of inheritance, there can be two or more classes, in which the first class is inherited in second class and second class is inherited in the third class and so on if more class available.
in this, the first class is derive class for the base class and the second class whose member inherits it for the next class (in which its members are inherited).

Here, we are take three class, see below

Example:-

In this, the first class is taking two numbers from the user and the second class will calculate these numbers while the third class is printing the total of these numbers.

multi-level-inheritance

Here is the program,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class first          // base class
{
  protected:
  int a,b;
  public:
  void get_num(int x,int y)
  {
     a = x;
     b = y;
  }
}; // first class terminate

class second:public first      // class first inherit in class second
{
  protected:
  int sum;
  public:
  void get_sum()
  {
    sum = a+b;
  }
}; //second class terminate

class third:public second       // class second inherit in class third
{
  public:
  void show_sum()
  {
    cout<<"Total: "<<sum;
  }
}; // third class terminate

void main() // main function start here
{
  int a,b;
  clrscr();

  third obj;  // class third object declare

  cout<<"Enter two number: ";
  cin>>a>>b;

  obj.get_num(a,b);    // class first member call
  obj.get_sum();           // class second member call
  obj.show_sum();         // class third member call

  getch();
}
OUTPUT:-
Enter two number: 5 6
Total: 11

 

Multiple inheritance

It has one or more base classes while only one derive class. That is, the property of all base classes is inherited in one single derive class.

SYNTEX:

class derive-class_name : mode base1-class_name,mode base1-class_name, ….n
{
member of derive class;
}

Example:-

In this, both, the first class and the second class take one number from the user while the drive class is counting these numbers and printing their total-

multiple-inheritance

Here is the program,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class first     // base class first
{
  protected:
  int a;
  public:
  void get_num(int x)
  {
      a = x;
  }
}; // first class terminate

class second  // base  class second
{
   protected:
   int b;
   public:
   void get_num1(int y)
   {
       b = y;
   }
}; //second class terminate

class third:public first,public second      // class first and class second inherit in class third
{
   int sum;
   public:
   void show_sum()
   {
       sum = a+b; // access first and second class member
       cout<<"Total: "<<sum;
   }
}; // third class terminate

void main() // main function start here
{
   int a,b,c;
   clrscr();

   third obj;      // class third object declare

   cout<<"Enter two number: ";
   cin>>a>>b;

   obj.get_num(a);    //  class first member call
   obj.get_num1(b);    // class second member call
   obj.show_sum();      // class third member call

   getch();
}
OUTPUT:-
Enter two number: 5 6
Total: 11

 

Hierarchical inheritance

This is in contrast to multiple inheritance, it has one or more derive classes, whereas only one is base class, that is, the property of a base class is inherited in all other derive classes.

Example:–

In this, first class user is taking two numbers and second class is printing a total of these numbers while the third class is printing multiplication of these number-

hierarchical-inheritance

Here is the program,

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class first // base class first
{
  protected:
   int a,b;
  public:
   void get_num(int x, int y)
   {
     a = x;
     b = y;
   }
}; // first class terminate

class second:public first // class first inherit in class second
{
   int sum;
  public:
   void show_sum()
   {
     sum = a+b; // access first and second class member
     cout<<“\nSecond class:”;
     cout<<“\nTotal: “<<a<<“+”<<b<<” = “<<sum<<endl;
   }
}; //second class terminate

class third:public first // class first inheit in class third
{
   int mul;
  public:
   void show_mul()
   {
     mul = a*b; // access first and second class member
     cout<<“\nThird class:”;
     cout<<“\nMultiplicatoin: “<<a<<“x”<<b<<” = “<<mul;
   }
}; // third class terminate

void main() // main function start here
{
  int a,b,c;
  clrscr();

  second obj1; // class second obj1 declare
  third obj2; // class third obj2 declare

  cout<<“Enter two number: “;
  cin>>a>>b;

  obj1.get_num(a,b); // class first member calling
  obj2.get_num(a,b); // class first member call again

  cout<<“First class : “;
   cout<<"\nNumber are: "<<a<<” “<<b<<endl;

  obj1.show_sum(); // class second member call
  obj2.show_mul(); // class third member call

  getch();
}
OUTPUT:-
Enter two number: 5 6

First class: 
 Number are: 5 6

Second class:
Total: 5+6 = 11

Third class:
Multiplication: 5x6 = 30

 

Hybrid inheritance

A hybrid inheritance is a collection of multiple and multi level inheritance. It has more than one base classes and more than one derive classes

hybrid-inheritance

Here is the program, below

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

class first         //base class first
{
  protected:
  int a,b;
  public:
  void get_num(int x, int y)
  {
	a = x;
	b = y;
  }
}; // first class terminate

class second:public first   // first class inherit in second
{
  protected:
  int sum;
  public:
  void get_sum()
    {
      sum = a+b;
    }
}; //second class terminate

class third        //base class thirst
{
  protected:
  int m,n,mul;
  public:
  void get(int x,int y)
   {
      m = x;
      n = y;
      mul = m+n;
   }
};    // third class terminate

class fourth:public second,public third  // second and third class inherit
{
  public:
  void show_all()
  {
	cout<<"\nTotal: "<<a<<"+"<<b<<" = "<<b<<sum;
	cout<<"\nMultiplicatoin: "<<m<<"x"<<n<<" = "<<mul;
  }
};     //fourth class terminate

void main()    // main function start here
 {
      int a,b;
      clrscr();

      fourth obj;       // fourth class object declare

      cout<<"Enter two number: ";
      cin>>a>>b;

      obj.get_num(a,b);      // class first member
      obj.get(a,b);        // class second member call
      obj.get_sum();       // class second member call
      obj.show_all();  // class fourth member call

   getch();
 }
OUTPUT:-
Enter two number: 5 6
Total: 5+6 = 11
Multiplication: 5x6 = 30


Did you notice

If you have noticed, then you must have noticed that here only the object of the derive class (except the program of hierarchical inheritance) is created in inheritance, but it is not necessarily that the object of base class is not created. Because here, the program is easily presented. It means to say that here we have inherited all the properties of the base class in the derive class so that there is no need to create the object of the base class.

We know that, in inheritance we inherit the property of one class in another class but also from a nested class we can add the property of one class to another class, but the two differ slightly. In inheritance, we add some specific property of a base class to a derive class as per our requirement, that too without using the object of the base class means, direct access whereas in the nested class, only the property of an entire class Is added to another class, and also to access the members of that class, we have to use the object of that class.