Different Type Of Inheritance In C++ Programming

Types of Inheritance in C++

The process of inheriting properties of objects of one class by objects of another class. The class whose properties are inherited by another class is called Base or Parent or Super class and the class which inherits properties of another class is called Derived or Child or Sub class.

C++ supports six types of inheritance as follows :

  • Single Inheritance
  • Multilevel Inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Multipath Inheritance
  • Hybrid Inheritance

1. Single Inheritance

When a single class is derived from a single parent class, it is called Single inheritance. It is the simplest of all inheritance.

Syntax of Single Inheritance :

class A {
   properties; //A is the base class
   methods ;
};

class B: public A {
   properties; // B is the derived class
   methods ;
} ;

2. Multilevel Inheritance

When the derived class inherits from a class, which in turn inherits from some other class, it is called Multilevel inheritance. The Super class for one, is sub class for the other.

Syntax of multilevel Inheritance:

class A {
	properties ; //A is the base class
	methods ;
};

class B : public A {
	properties ;  //class B is derived from class A
	methods ;
};

class C: public B {
	properties ;  //class C is derived from class B
	methods ;

};

class D: public C {
	properties; //class D is derived from class C
	methods ;
};

3. Multiple Inheritance

When one class is inherited from more than one base class, it’s known as Multiple inheritance.

Syntax of Multiple Inheritance:

class A {
	properties ; //A is the base class
	methods;
};

class B {
	properties; //B is the base class
	methods;
};

class C {
	properties ; //C is the base class
	methods;
};

class D: public A, public B, public C {
	properties; //class D is derived from the class A,B,C
	methods;
} ;

4. Hierarchical Inheritance

When more than one class is inherited from the base class, it’s known as Hierarchical inheritance.

Syntax of Hierarchical Inheritance:

class A {
	properties; //A is the base class
	methods;
};

class B: public A {
	properties ;  //class B is derived from the class A
	methods;
};

class C: public A {
	properties; //class C is derived from the class A
	methods;
} ;

5. Multipath Inheritance

When a derived class has two base classes and these two base classes have one common base class, it is called multipath inheritance

Syntax of Multipath Inheritance:

class A {
	properties; //A is the base class
	methods ;
};

class B: virtual public A {
	properties;  //class B is derived from the class A
	methods;
};

class C: virtual public A {
	properties ; //class C is derived from the class A
	methods;
};

class D: public B, public C {
	properties; //class D is derived from the class B and C
	methods;
};

NOTE: Virtual must be used to overcome multipath problem.

6. Hybrid Inheritance

This is the combination of more than one inheritance. It may be combination of Multilevel and Multiple inheritance or Hierarchical and Multilevel inheritance or Hierarchical and Multipath inheritance or Hierarchical, Multilevel and Multiple inheritance.

Syntax of Hybrid Inheritance:

class A {
	properties; //A is the base class
	methods;
};

class B: public A {
	properties ; //class B is derived from class A
	methods;
};

class C: public B {
	properties;  //class C is derived from class B
	methods;
};

class D: public A, public C {
	properties ; //class D is derived from the class A and C
	methods;
};

Leave a Reply