Types Of Inheritance



One of the most important concepts in object-oriented programming is that of
inheritance. Inheritance allows us to define a class in terms of another class,

Types Of Inheritance

1.Single Inheritance
2.Multiple Inheritance
3.Hierarchical Inheritance
4.Multilevel Inheritance
5.Hybrid Inheritance (also known as Virtual Inheritance)

1. Single Inheritance

In this type of inheritance one derived class inherits from only one base class.
It is the most simplest form of Inheritance.



Syntax:

class base_classname
{
    properties;
    methods;
};

class derived_classname : visibility_mode base_classname
{
    properties;
    methods;
};

Example:

#include <iostream.h>
#include  <conio.h>
using namespace std;

class person    /*Parent class*/
{
  private:
      char fname[100],lname[100],gender[10];
  protected:
      int age;
  public:
      void input_person();
      void display_person();
};

class student: public person    /*Child class*/
{
  private:
      char college_name[100];
      char level[20];
  public:
      void input_student();
      void display_student();
};

void person::input_person()
{
    cout<<"First Name: ";
    cin>>fname;
    cout<<"Last Name: ";
    cin>>lname;
    cout<<"Gender: ";
    cin>>gender;
    cout<<"Age: ";
    cin>>age;
}

void person::display_person()
{
    cout<<"First Name : "<>level;
}

void student::display_student()
{
    person::display_person();
    cout<<"College    : "<

Output

Input data
First Name: Harry
Last Name: Potter
Gender: Male
Age: 23
College: Abc International College
Level: Bachelors

Display data
First Name : Harry
Last Name  : Potter
Gender     : Male
Age        : 23
College    : Abc International College
Level      : Bachelors


2. Multiple Inheritance

In this type of inheritance a single derived class may inherit from two or more than two base classes.




Syntax:

class base_class1
{
    properties;
    methods;
};

class base_class2
{
    properties;
    methods;
};
... ... ...
... ... ...
class base_classN
{
    properties;
    methods;
};

class derived_classname : visibility_mode base_class1, visibility_mode base_class2,... ,visibility_mode base_classN
{
    properties;
    methods;
};

Example:

#include <iostream.h>
#include <conio.h>
using namespace std;

class liquid
{
    float specific_gravity;
    public:
        void input()
        {
            cout<<"Specific gravity: ";
            cin>>specific_gravity;
        }
        void output()
        {
            cout<<"Specific gravity: "<>rate;
        }
        void output()
        {
            cout<<"Rate(per liter): $"<

Output

Enter data
Specific gravity: 0.7
Rate(per liter): $0.99

Displaying data
Specific gravity: 0.7
Rate(per liter): $0.99


3. Hierarchical Inheritance

In the Hierarchical Inheritance, there is only base class and all the derived classes are derived from this base class..
The following is the schematic representation. Class A is the base class and class B and class C are the derived classes.
classes are derived from the base class. Have a look at the following example.

Syntax:

class base_classname
{
    properties;
    methods;
};

class derived_class1:visibility_mode base_classname
{
    properties;
    methods;
};

class derived_class2:visibility_mode base_classname
{
    properties;
    methods;
};
... ... ...
... ... ...
class derived_classN:visibility_mode base_classname
{
    properties;
    methods;
};

Example:

#include <iostream.h>
using namespace std;
class base{
  public:
  void rbc1(){
    cout<<"base - rbc1"<

Output

base - rbc1
base - rbc1
der1 - rbc2
base - rbc1
der2 - rbc3


4. Multilevel Inheritance

In C++ programming, not only you can derive a class from the base class but you can also ..
derive a class from the derived class. This form of inheritance is known as multilevel inheritance..

Syntax:

class A
{ 
... .. ... 
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};

Example:

#include <iostream.h>
using namespace std;
class Student{
    protected:
    int marks;
    
    public:
    void accept(){
        cout<<" Enter marks";
        cin>>marks;
        
    }
};
 
class Test :public Student{
    protected:
    
    int p=0;
    
    public:
    void check(){
        if(marks>60){
            p=1;
        }
    }
};
 
class Result :public Test{
    public:
    void print(){
        if(p==1)
        cout<<"\n You have passed";
        else
        cout<<"\n You have not passed";
    }
};
int main(){
    Result r;
    
    r.accept();
    r.check();
    r.print();
    
  return 0;
}

Output

Enter marks 70
You have passed



5. Hybrid Inheritance

There could be situations where we need to apply two or more types of inheritance combined to ..
design a program, this situation is called Hybrid Inheritance. Basically Hybrid Inheritance is the combination of one or more types of the inheritance...

Syntax:

class A                    // Grandparent
{ ..
...
 };
class B : virtual public A                 // Parent1
{ ..
...
 };

class C : public virtual A                 // Parent2
{ ..
... 
};

class D : public B, public C              // Child
{ ..
.. 
};


Example:

#include <iostream.h>
using namespace std;
class base1{
    public:
    
    void msg1(){
        cout<<"base1 - msg1"<

Output

base1 - msg1
base2 - msg2
base1 - msg1
der1 - msg3
base1 - msg1
base2 - msg2
der1 - msg3
der2 - msg4




                        <<  PREVIEW  >>



                        <<   NEXT    >>





Comments