Inheritance In C++

aimtocode.com

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


aimtocode.com

1) 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.

aimtocode.com

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

Syntax:

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

Example 1:

Output :

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

2) Multiple Inheritance ?

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

aimtocode.com

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

Syntax:

aimtocode.com

Example 1:



Output :

   x: 11
   value of a: 100
   value of b: 200
   value of c: 300
aimtocode.com

3) Multilevel Inheritance ?

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

aimtocode.com

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.

Syntax:

aimtocode.com

Example 1:



Output :

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

4) 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.

aimtocode.com

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.

Syntax:

aimtocode.com

Example 1:



Output :

x: 11
This is a Vehicle
This is a Vehicle


aimtocode.com

5) 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..

aimtocode.com

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

Syntax:

aimtocode.com

Example 1:



Output :

 x: 11
Sum= 14
aimtocode.com