Types of Inheritance
Types of Inheritance
C++ is very flexible in inheritance and provides the different types of inheritance including Single, Multiple, Hierarchical, Multilevel and Hybrid Inheritance.
Different computer languages offer different types of inheritance. However the basic idea remains same that derived class gets access to the (non-private) members of the base class. C++ is very flexible in inheritance and provides the following types of inheritance.
This is the basic type of inheritance where the base and derived classes remain one i.e. one class is inherited by exactly one class. It means that there will be only one child and one parent only.
As shown above, if a parent class has exactly one child class and the child class has exactly one parent class then this kind of inheritance is called single inheritance.
In this type of inheritance a child class has more than one parent classes and hence inheriting more than one base class. Advantage of this kind of inheritance is that one child class accesses the members of more than one parent classes thus taking full advantage of inheritance. This is a complex type of inheritance and some computer languages (like Java, C#, VB.Net etc.) do not support this kind of inheritance but as C++ is very flexible in inheritance, so supports multiple inheritances as well. The following figure shows that how it takes place.
In this type of inheritance, a single parent class has more than one child class. It allows to access the code of one class in more than one derived class. Almost all computer languages supporting object oriented programming support hierarchical inheritance. The following figure depicts this kind of inheritance.
As the name suggests, the inheritance can be extended to more than two levels. In this kind of inheritance the same class may be child of the parent class and parent of its child class. In the following figure Class2 is the child of Class1 and at the same time is parent of Class3.
Hybrid (Virtual) Inheritance
This kind of inheritance is collection of different types of inheritance discussed above. For example a child class may have more than one parents and a class may have more than one children in multi levels as explained in the figure below.
As shown in the figure above, Class1 has more than one child classes and Class5 has more than one parent classes. There is multi-level inheritance too. You can combine any kind of inheritance discussed earlier to form the hybrid inheritance.
More Related Articles For You
- What is C++
- C++ and Object Oriented Programming OOPS concepts
- Syntax and Structure of C++ program
- Data Types in C++
- C++ Variables
- Types of operators in C++
- Decision making in C++
- C++ Loop Types
- Storage Classes in C++
- Functions in C++
- Classes and Objects in C++
- Access controls in C++ Classes
- Defining Class and Object in C++
- Accessing Public and Private Data Members in C++
- Member Functions in Classes
- Types of Member Functions in C++
- Inline Functions in C++
- Namespaces in C++
- Function Overloading in Classes in C++
- Constructors and Destructors in C++
- Static Keywords in C++
- Const Keyword in C++ Programming
- References in C++
- Copy Constructor in C++
- Pointer to Members in C++ Classes
- Introduction to Inheritance
- Order of Constructor Call in Inheritance
- Upcasting in C++
- Function Overriding in C++
- Virtual Functions in C++
- Abstract Class and Pure Virtual Functions in C++
- Virtual Destructors in C++
- Operator Overloading in C++