Inheritance In C++

Inheritance ?

The technique of deriving a new class from an old one is called inheritance. The old class is referred to as base class and the new class is referred to as derived class or subclass. Inheritance concept allows programmers to define a class in terms of another class, which makes creating and maintaining application easier.

When writing a new class, instead of writing new data member and member functions all over again, programmers can make a bonding of the new class with the old one that the new class should inherit the members of the existing class.

Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application.


Types of Inheritance?

Inheritance is Basically classified into to five different types of inheritance:

Single Inheritance

Multiple Inheritance

Multilevel Inheritance

Hierarachical Inheritance

Hybrid Inheritance


1)C++ Single Inheritance ?

If a single class is derived from one base class then it is called single inheritance. In C++ single inheritance base and derived class exhibit one to one relation.

As shown in the figure, in C++ single inheritance only one class can be derived from the base class.

C++ Single Inheritance : Syntax:

class subclass_name : access_mode base_class
{
	.........
	.........
  //body of subclass
};

C++ Single Inheritance : Example

Output :

x: 11
Enter the value of x = 3
Enter the value of y = 4
Product = 12

2) C++ Multiple Inheritance ?

When a class is derived from two or more base classes, such inheritance is called Multiple Inheritance.

Multiple inheritance occurs when a class inherits from more than one base class.The class which inherits the properties of another class is called Derived or Child or Sub class and the class whose properties are inherited is called Base or Parent or Super class

C++ Multiple Inheritance :Syntax

C++ Multiple Inheritance: Example



Output :

   x: 11
   value of a: 100
   value of b: 200
   value of c: 300

3)C++ Multilevel Inheritance ?

In Multilevel inheritance, there is one base class and another class which is derived from this base class is called derived class.

As shown in above block diagram, class C has class B and class A as parent classes. Depending on the relation the level of inheritance can be extended to any level.

C++ Multilevel Inheritance : Syntax

C++ Multilevel Inheritance: Example



Output :

x: 11
Total Objects: 1
Value of a: 10
Value of b: 20
Value of c: 30

4) C++ Hierarcical Inheritance ?

In this type of inheritance, more than one sub class is inherited from a single base class. i.e. more than one derived class is created from a single base class.

As shown in above block diagram, in C++ hierarchical inheritance all the derived classes have common base class. The base class includes all the features that are common to derived classes.

C++ Hierarcical Inheritance : Syntax

C++ Hierarcical Inheritance : Example



Output :

x: 11
This is a Vehicle
This is a Vehicle


5) C++ Hybrid Inheritance ?

Basically C++ hybrid inheritance is combination of two or more types of inheritance. Which means hybrid inheritance is implemented by combining more than one type of inheritance.
For example:
Combining Hierarchical inheritance and Multiple Inheritance..

The inheritance in which the derivation of a class involves more than one form of any inheritance is called hybrid inheritance.

C++ Hybrid Inheritance Syntax:

C++ Hybrid Inheritance Example :



Output :

 x: 11
Sum= 14