C++ Inheritance basics
In this chapter we will be learning about C++ Inheritance basics. Tutorial explains you different types of inheritance in C++ programming language.
Table of content
Basics of inheritance
Inheritance is one of the basic features of object oriented programming. In the process of inheritance, one object can acquire the properties of another class.
Type of class | Definition |
---|---|
Base Class | A class that is inherited is called a base class. In below diagram, the class 'vehicle ' is a base class. |
Derived Class | The class that does the inheriting is called a derived class. In below diagram, the class 'car ' is a derived class. |
Explanation
- In the above picture, it is clearly mentioned that we are having two classes i.e base and derived class.
- Base class contains general information about the specific entity
- Derived class contains more specific information related to the one of the type which is inherited from the base class.
- In above example
vehicle
class will contain general information about all vehicles but derived classcar
contains all the information of the classvehicle
along with car specific information.
Types of Inheritance
According to the way of inheriting the properties of base class, there are certain forms of inheritance.
Inheritance Type #1 : Single Inheritance
In this type of inheritance, one Derived class is formed by acquiring the properties of one Base class.
Class Name | Type | Explanation |
---|---|---|
A | Base class | It is base class in the above program |
B | Derived class | Class derives the properties of class A |
Inheritance Type #2 : Multiple Inheritance
In this type of inheritance, one Derived class is formed by acquiring the properties of multiple Base classes
Class Name | Type | Explanation |
---|---|---|
A | Base class | It is base class in the above program |
B | Base class | It is base class in the above program |
C | Derived class | Class derives the properties of class A and B |
Inheritance Type #3 : Hierarchical Inheritance
In this type of inheritance, multiple subclass are formed by acquiring the properties of one Base class.
Class Name | Type | Explanation |
---|---|---|
A | Base class | It is base class in the above program |
B | Derived class | Class derives the properties of class A |
C | Derived class | Class derives the properties of class A |
D | Derived class | Class derives the properties of class A |
Inheritance Type #4 : Multilevel Inheritance
In this type of inheritance, derived class is formed by acquiring the properties of one base class.
Again same derived class is used as a base class and another class is derived from this.
Class Name | Type | Explanation |
---|---|---|
A | Base class | It is base class in the above program |
B | Base class for C and derived class for A | - |
C | Derived class | It contains all the properties of class A and B so we can consider this as derived class of A also |
Inheritance Type #5 : Hybrid Inheritance
In this type of Inheritance, the Derived class is formed by any legal combination of above four forms.