Inheritance in C++ programming language
In this tutorial, we will discuss the OOP concept of Inheritance in C++ programming language.
Inheritance is one of the Oop concepts in object-oriented programs such as C++, java. It is where one object acquires all the properties of (data members and methods) it’s the parent or base class.
It means the new class inherits some of the properties and behaviour of the existing class. The new class is the child or derived class and the existing class is known as the parent class.
It is the process of reusing the properties of an existing class without modifying them.
Existing class – Base class – The base class has its own properties and functionality. It’s also known as the parent class. It has the common qualities that all the objects can inherit.
New class – Derived class – The derived class is the child class. The derived class inherits the properties and functionality of the base class.
Advantages in inheritance
- Inheritance means code reusability The existing class or base class can be used by the derived class without any changes . Hence, when a code is created, it can be used by many programmers for their use.
- Data abstraction – Derived class can add its own data and member function to the class without affecting the base class.
- No need to define the data members again.
- Inheritance in C++ takes place between classes.
Type of inheritance in C++
- Single inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Multiple inheritances
- Hybrid inheritance
Single inheritance
Single inheritance is easy to understand as class extends to only one class (Single child is derived from single parent). Single inheritance only has one parent and child class. Child class has its own property as well as those authorised from the parent class. The diagram below shows that only B extend from A. Here, A is the parent class or base class and B is the derived or child class.
Single inheritance in C++ with example program
class A{ //parent class
Block of code.
………………..
};
public class B: public A{ //child class
Block of code.
………………..
}
Multilevel inheritance
Multilevel means one class(derived) extends from two or more (more than one) classes(base). A class can be the child (derived) class as well as the parent class to other classes. In the diagram below, Class B inherits the property of class A and Class C inherits the property of class B. Again,
in order, Class A is the parent to Class B and Class B is the parent to Class C.
Multilevel inheritance in C++ with Example
class A{ //parent class
Block of code.
………………..
};
class B: public A{ //parent class or child class
Block of code…
……………………
};
class C :public B{//child class
Block of code.
……………………
};
……may be continue…
..
Multiple inheritance
In multiple inheritance, one derived class extends from more than one parent class. In multiple inheritance, one child class has two or more parent classes. In the diagram below, Class C inherits the properties of class A and Class B.
Multiple inheritance in C++ with Example
Public class A{
Block of code…
}
public class B {
Block of code…
}
public class C : A,B{
Block of code…
}
Hierarchical inheritance
In the hierarchical inheritance, one parent class can be inherited by many children classes. In the example below, class A is inherited by Class B and Class C. Class A acts as the base class for class B and class C.
Hierarchical inheritance with example in C++
Public class A{
Block of code…
}
public class B extends A{
Block of code…
}
public class C extends A{
Block of code…
}
Hybrid inheritance in C++
Hybrid inheritance is different from other inheritance. It is a combination of single, multilevel, multiple, and hierarchical inheritance.