Inheritance Part 2: Types of Inheritance

 

Types of inheritance:

Types of inheritance are

1.    Single Inheritance

If a class derived from a single base class, it is called as single Inheritance.



Ex:

#include<iostream.h>

#include<conio.h>

class student

{

     private:

                 int regno;

                 cha name[20];

     public:

                 viod getdata()

                 {

                             cin>>regno>>name;

                 }

                 void display()

                 {

                             cout<<regno<<name;

                 }

};

class marks:pubic student

{

     private:

                 int m1,m2,total;

     pubic:

                 void getdata1()

                 {

                             cin>>m1>>m2;

                 }

                 void display1()

                 {

                             cout<<m1<<m2<<total;

                 }

};

void main()

{

     marks m;

     clrscr();

     m.getdata();

     m.getdata1();

     m.display();

     m.dispay1();

     getch();

}

2.    Multilevel inheritance:

The classes can also be derived from the classes that are already derived, it is called multilevel inheritance.

Or

A class can be derived from another derived class is called multilevel inheritance.


Ex:

class book

{

                 ----

  };

  class publication:public book

  {

                 ----

  };

  class volume:public publication

  {

                 ----

  };

3.    Multiple inheritance:

        If a class derived from more than one class, it is known as multiple inheritances.




Ex:

class bank

{

     ----

};

class customer

{

     ----

};

class accounts:pubic bank, public customer

{

     ----

};

 

4.    Hierarchical inheritance:

A number of classes are derived from a single base class; it is called as hierarchical inheritance.



Ex:

class science

{

     ----

};

class compsc:public science

{

     ----

};

class biology:public science

{

     ----

};

class electronics:pubic science

{

     ----

};

5.    Hybrid inheritance:

Hybrid inheritance is combination of multilevel and multiple inheritances.




Ex:

class college

{

     ----

};

class classroom:public college

{

     ----

};

class library

{

     ----

};

class student:public classroom, public library

{

     ----

};


Comments