Types of inheritance single, multiple, multilevel, hybrid in c++
In C++ object-oriented programming (OOPS) concepts, inheritance allows a subclass (derived class) to inherit the properties and default behavior of another root superclass (base class) and apply it to derived classes or subclasses. C++ programming supports multiple types of inheritance concepts, including single inheritance, multiple inheritance, multilevel inheritance, hybrid inheritance, etc. These are used in C++ to represent different relationship models between root and subclass classes.

Key functions and features of the inheritance concept in C++.
- Single inheritance.
- Multiple inheritance.
- Multilevel inheritance.
- Hybrid inheritance.
So, let’s learn more about each type of inheritance, its functions, features, and concepts in C++, with detailed examples and explanations.
Single inheritance concept in C++.
In the single inheritance concept in a C++ program, a subclass (derived class) inherits from a single base or root (parent) class. This is the most basic and common method of single inheritance, in which a subclass is directly derived from a parent or another class.
Example of the single inheritance concept.
#include <iostream>
#include <string>
class Course { // Here we define a course name. Base class
public:
void select() {
std::cout << “Select your desired course” << std::endl;
}
};
class Cplus : public Course { // Cplus Derived class defined here
public:
void selection() {
std::cout << “C++ course selection” << std::endl;
}
};
int main() {
Cplus testCplus;
testCplus.select(); // here it is inherited from the Course base class
testCplus.selection(); // here it is defined in the Cplus class base class
return 0;
}
Explanation of the single inheritance concept.
- In the single inheritance example, Course is the base or root class, which defines the select() method.
- Similarly, Cplus is a derived class created, which inherits from the Course base class using public inheritance.
- The Cplus class can access the select() method from the base class, and it can also define its own custom class method, selection().
Multiple inheritance concept in C++.
In the multiple inheritance concept in a C++ program, a user-defined subclass (derived class) inherits from more than one base or parent class. This allows a user-defined derived class to inherit class member parameters, attributes, and behavior properties from multiple root or base classes.
Example of the multiple inheritance concept.
#include <iostream>
class Course { // Course name created from the Base or root class
public:
void select() {
std::cout << “Select your desired course” << std::endl;
}
};
class Cplus { // Cplus name created from the Base or root class
public:
void Cselection() {
std::cout << “C++ course selection” << std::endl;
}
};
class Java : public Course, public Cplus { // Derived class created from both Course and Cplus classes
public:
void Jselection() {
std::cout << “Java course selection” << std::endl;
}
};
int main() {
Java testJava;
testJava.select(); // here it is inherited from Course base class 1
testJava.Cselection(); // here it is inherited from Cplus base class 2
testJava.Jselection(); // here it is defined in Java Derived class
return 0;
}
Explanation of the multiple inheritance concept.
- In the multiple inheritance example here, both Course and Cplus are defined as base or parent classes.
- Java is defined as a derived class that inherits class behavior and properties from both Course and Cplus classes using the multiple inheritance concept.
- The Java class can access methods from both Course (e.g., select()) and Cplus (e.g., Jselection()) in addition to its own Jselection() method.
Potential issues with using multiple inheritance in C++.
Diamond Problem If a user-defined class declares a common base class for two or more base classes, then derived or subclasses defined in that class may inherit from the same root or base class multiple times. This can lead to ambiguity issues in existing class function method calls.
To address this issue, virtual inheritance is used later in C++ programs to ensure that derived classes receive only one copy of the data and methods of a common base class when needed.
Multilevel inheritance concept in C++.
In a C++ program, the multilevel inheritance concept involves a user-defined class (derived class) inheriting from other subclasses or derived classes. This creates a chain hierarchy of inheritance, creating a multi-level derived class concept. In this case, a derived class can be used as a base class for another derived class.
Example of multilevel inheritance concept.
#include <iostream>
class Course { // Here the course name is the base or root class created.
public:
void select() {
std::cout << “Select your desired course” << std::endl;
}
};
class Cplus : public Course { // Cplus name Derived class 1 from Course class
Public:
void Cselection() {
std::cout << “C++ course selection” << std::endl;
}
};
class Java : public Cplus { // Java name Derived class 2 from Cplus class
Public:
void Jselection() {
std::cout << “Java course selection” << std::endl;
}
};
int main() {
Java testJava;
testJava.select(); // here it Inherited from Course main base class
testJava.Cselection(); //here it Inherited from Cplus derived class
testJava.Jselection(); // here it is defined in Java derived class
return 0;
}
Explanation of multilevel inheritance concept.
- In this multiple inheritance example, Course is defined as the base or root class.
- Cplus is a connected subclass derived from Course.
- Cplus is a class derived from Java, which itself inherits from the Course class.
- A Java class can access its behavior properties and methods from both Cplus (e.g., Cselection()) and Course (e.g., select()) classes.
Hybrid inheritance concept in C++.
In the hybrid inheritance concept in a C++ program, a user-defined class inherits behavior properties and methods from more than one class by using multiple individual types of inheritance (e.g., a group combination of multiple, multilevel, or single inheritance).
Example of the hybrid inheritance concept.
#include <iostream>
class Course { // Course Base or root class created class 1
Public:
void select() {
std::cout << “Select your desire course” << std::endl;
}
};
class Cplus { // Cplus Base or root class created class 2
Public:
void Cselection() {
std::cout << “C++ course selection” << std::endl;
}
};
class BasicCourse { // BasicCourse Base or root class created class 3
Public:
void Bcourse() {
std::cout << “Basic course selection” << std::endl;
}
};
class Java : public Course, public Cplus, public BasicCourse { // here it is a derived class from multiple above Course, Cplus, and BasicCourse class bases
public:
void Jselection() {
std::cout << “Java course selection” << std::endl;
}
};
int main() {
Java testJava;
testJava.select(); // here it is inherited from Course main base class
testJava.Cselection(); // here it is inherited from Cplus main base class
testJava.Bcourse(); // here it is inherited from BasicCourse main base class
testJava.Jselection(); // here it is defined in Java derived class
return 0;
}
Explanation of the hybrid inheritance concept.
- In the hybrid inheritance example, Course, Cplus, and BasicCourse are three different base or root classes defined.
- In which Java itself is a derived class of Course, Cplus, and BasicCourse. It inherits its behavior, properties, and methods from all three base classes.
- The Java class can access and inherit class methods from all three base classes (select(), Cselection(), Bcourse()) in addition to its own Jselection() method.

