What is inheritance ? What are the different forms of inheritance ? Or What is inheritance ? Explain its various types. Or Explain inheritance. Explain various types of inheritance with example.

What is inheritance ? What are the different forms of inheritance ? Or What is inheritance ? Explain its various types. Or Explain inheritance. Explain various types of inheritance with example.

Ans. Inheritance is the process b y which objects of one class acquire the properties of objects of another class, i.e., inheritance is the process of creating new classes called derived classes, from the existing classes, called base classes.

The derived classes inherit all or some of the properties of the base classes, but can add their  own properties. In this process, the properties of base classes are unchanged.

The general form of defining a derived class is as follows –

Class derived_class : visibility_more base_class

{

………..// members of derived class

………..

};

Here the colon indicates that derived_class is derived  from the base_class. The visibility_more is optional, and if it present, may be either private or public. The default visibility mode is private.

 

For example,

Class ABC : private XYZ //

Private derivation

{

Members of ABC

};

Class ABC : public XYZ  //

Public derivation

{

Members of ABC

};

There are following forms of the inheritance –

  • Single inheritance
  • Multiple inheritance
  • Hierarchical inheritance
  • Multilevel inheritance
  • Hybrid inheritance.
  • Single Inheritance – A derived class only one base class, is called single inheritance. That is,

It shows B is a base class and D is a derived class.

The general form of defining it as follows –

Class derived_class : visibility_mode base_class

{

………..

………..

};

 

 

 

 

 

 

  • Multiple Inheritance – A derived class with several base classes is called the multiple inheritance. That is,

It allows to combine the features of B1 and B2 in the derived  class D with new ones.

The syntax of multiple classes is as follows –

Class D : visibility B1, visibility B2

{

……..

……..//(Body of class D)

};

#Include<iostream.h>

Using namespace std;

Class M

{

Protected:

Int m;

Public:

Void get_m(int);

};

Class N

{

Protected:

Int n;

Public:

Void get_n(int);

};

Class P : public M, public N

{

Public:

Void display(void);

};

Void M :: get_m(int x)

{

M = x;

}

Void N :: get_n(int y)

{

N = y;

}

Void P:: display(void)

{

Cout<<”m”<<m<<”n”;

Cout<<”n=”<<n<<”n”;

Cout<<”m*n=”<<m*n<<”n”;

}

Int main( )

{

P p;

p.get_m(10);

p.get_n(20);

p.display( );

return 0;

}

  • Hierarchical – The features of one class may be inheritance bu more than one class. It is called the hierarchical inheritance.

Inheritance can be used to modify a class when it did not satisfy the requirements of a particular problem. Additional members are added through inheritance to extend the capabilities of a class.

In C++, such problem can be easily converted into class hierarchy. The base class includes all the features that are common to the subclasses. A subclass can be constructed by inheriting the properties of the base class. A subclass can serve as  base class for the level classes.

 

  • Multilevel Inheritance – The mechanism of deriving a class from another “derived class”, is called the multilevel inheritance.

Here, class A  serves as a base class for the derived class, B, which in turn serves as a base class formthe derived class C. the class B is known as intermediate base class. Since it provides a link for inheritance between A and C. the chain ABC is known as inheritance path.

The derived class with multilevel inheritance is declared as follows –

Class A                                                                                                      //Base class

{

……….

};

Class B: public A                                                                                    //B derived from A

{

……….

};

Class C :: public B                                                                                 //C derived from B

{

………

};

This process can be extended to any number of levels.

For example,

#include<iostream.oh>

Class stud

{

Protected :

Rollno;

Public :

Void getno(void);

Void putno(void);

};

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *