C++ Inheritance Types

As we have learned so far, inheritance is one of the main components of object-oriented programming (OOP). Thanks to this functionality, a class can acquire the traits and properties of another class. Depending on the visibility option selected, the base class’s data members can be accessible after being copied via inheritance. Accessibility is always arranged in descending order, from public to protected.


C++ Inheritance Types

This lesson will primarily cover 3 different types of inheritance in C++. We have already learned “Single-Level Inheritance” in the previous lesson. These are what they are:

  • Single-Level Inheritance
  • Multilevel Inheritances
  • Multiple Inheritance

C++ Multilevel Inheritance

Multilevel Inheritance means we can derive a class from a previous class of another class. It is the process by which you can derive a class from another descended class. A, B, and C are the three classes, let’s say.

According to the defined visibility modes, each respective derived class has access to the data members of its respective base class. The base class that class B derives from is A. B is A’s derived class. The class that comes from class B now is C. As a result, class B becomes the foundation class for class C while also deriving from class A. Multilevel Inheritance is the name given to this situation.

Multilevel Inheritance Illustration

In the example below, GrandChildClass is a descendant of the class ChildClass (derived from ParentClass).

C++ Multilevel Inheritance Example

#include <iostream>

using namespace std;
// Parent class
class ParentClass {
  public:
    void multilevelInheritance() {
      cout << "C++ Multilevel Inheritance";
    }
};

// Child class
class ChildClass: public ParentClass {};

// Grandchild class
class GrandChildClass: public ChildClass {};

int main() {
  GrandChildClass Obj;
  Obj.multilevelInheritance();
  return 0;
}

Output

C++ Multilevel Inheritance

C++ Multiple Inheritance

Multiple inheritance is the inheritance process in which a class can take traits from more than one base class, or a derived class can have more than one source class. At the point of inheritance, it provides access specifiers independently for each base class. The derived class can derive the combined features of all these classes, and the derived or child class can access the data members of all the base classes by the access specifiers.

C++ Multiple Inheritance

The following example illustrates Multiple Inheritance in C++:

C++ Multiple Inheritance Example

#include <iostream>
using namespace std;
// Base class
class baseClass1 {
  public:
    void baseClassFun() {
      cout << "Multiple Inheritance from Base Class1.\n";
    }
};
// Another base class
class baseClass2 {
  public:
    void baseClass2Fun() {
      cout << "Multiple Inheritance from Base Class2.\n";
    }
};
// Derived class
class MyChildClass: public baseClass1, public baseClass2 {};
int main() {
  MyChildClass obj;
  obj.baseClassFun();
  obj.baseClass2Fun();
  return 0;
}

Output

Multiple Inheritance from Base Class1.
Multiple Inheritance from Base Class2.

This concludes the C++ Inheritance Types lesson. In The next lesson, you will learn about Polymorphism in C++ and its usage.