More in inheritance….

We shall continue our course in inheritance. We learnt about syntax, terminology, single inheritance in our last post. Today we shall learn about remaining types of inheritance.

Multilevel Inheritance: In single inheritance base class is inherited by only one derived class. Here that derived class is again inherited by other class assuming it as base class. The Graphical representation of the multi level inheritance is:

multi level

Let us check an example program for inheritance:

#include iostream.h
#include conio.h
class base
{
  Public:
  int i;
};
class derived1:public base
{
  Public:
  int j;
};
class derived2: public class derived 1
{
  Public:
  int sum;
};
Void main()
{
  clrscr();
  derived2 ob;
  ob.i=2;
  ob.j=5;
  ob.sum=ob.i+ob.j;
  cout<<"Sum is: "<<ob.sum;
  getch();
}

From the above example we came to know that declaration of a multi-level inheritance is same as that of single inheritance except that we use derived class as a base class.

Multiple-inheritance: In this type of heritance a derived class is derived from two or more base class. i.e. a derived class contains the features of two base classes and it’s own if any. Since, derived class inherits two classes then there is possibility of ambiguity (duplication) of data members and member functions, solution for this will be discussed in the next session. Graphical representation of this inheritance is:

ple

Consider an example for multiple inheritance:
#include
#include
class base1
{
   Public:
  int i;
};
class base2
{
  Public:
  int j;
};
class derived:public base1,public base2
{
  Public:
int sum;
};
Void main()
{
 clrscr();
 derived1 ob;
 ob.i=5;
 ob.j=2;
 ob.sum=ob.i+ob.j;
 cout<<”Sum of numbers is:”<<ob.sum;
 getch();
}

We can observe that we are able to access data members of base1 and base 2 with object of derived class. That is the best advantage of inheritance.

Hierarchical inheritance: In this inheritance a base class is inherited by many derived classes. i.e. two or more derived class inherits the same base class. The graphical representation of hierarchical inheritance is:

hie

Now we shall go through an example:
#include
#include
class base
{
    Public:
    int i;
}; 
class derived:public  base
{
Public:
int j;
int sum;
};
class derived:public base
{
  Public:
   int k;
int mul;
};
Void main()
{
   Clrscr();
derived1 ob;
derived2 ob1;
ob.i=2;
ob.j=3;
ob.sum=ob.i+ob.j;
ob1.i=2;
ob1.k=3;
ob1.mul=ob1.i*ob1.k;
cout<<”sum is:”<<ob.sum;
cout<<”Mul is:”<<ob1.mul;
getch();
}

Therefore we are able to access base class member through derived1 and derived2.

Hybrid Inheritance: This is nothing but a combination of two or more types of inheritances.graphical representation of hybrid inheritance is:

hyb

There is nothing more to explain in it, so we shall directly go through the example:
#include
#include
class base
{
   public:
int i;
}
class derived1:public base
{
   public: 
  int j;
};
class derived2:public base,public derived1
{
  public:
int sum;
};
Void main()

 clrscr();
derived2 ob;
ob.i=2;
ob.j=3;
ob.sum=ob.i+ob.j;
cout<<”sum is:”<<ob.sum;
getch();
}
In this example we are using single inheritance and multiple inheritance.

Advertisement

About Anuroop D

Very enthusiastic about technology and likes to share my knowledge through blogging. Has Bachelor's in Information Technology and currently pursuing my PhD in Computer Science.
This entry was posted in C++ and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s