C++ Inheritance Explained | Single, Multilevel, Multiple, Hybrid and Virtual Base Class


This complete tutorial on C++ Inheritance explains how classes can derive properties and behaviors from other classes to promote code reuse. It covers single, multilevel, hierarchical, multiple, and hybrid inheritance, along with virtual base classes and the diamond problem. The tutorial follows best practices and helps beginners and intermediate programmers understand object-oriented relationships in C++.

Inheritance – Complete Tutorial

1. What is Inheritance?

Inheritance is a mechanism in C++ where a class (derived class) can acquire properties and behaviors from another class (base class).

It promotes code reuse, modularity, and hierarchical relationships.

2. Single Inheritance

A derived class inherits from a single base class.

Example:


#include <iostream>
using namespace std;

class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};

class Dog : public Animal {
public:
void bark() { cout << "Barking..." << endl; }
};

int main() {
Dog d;
d.eat(); // inherited
d.bark(); // own function
}

3. Multilevel Inheritance

A chain of inheritance where a class is derived from another derived class.

Example:


class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};

class Mammal : public Animal {
public:
void walk() { cout << "Walking..." << endl; }
};

class Dog : public Mammal {
public:
void bark() { cout << "Barking..." << endl; }
};

4. Hierarchical Inheritance

Multiple classes inherit from a single base class.

Example:


class Animal {
public:
void eat() { cout << "Eating..." << endl; }
};

class Dog : public Animal {
public:
void bark() { cout << "Barking..." << endl; }
};

class Cat : public Animal {
public:
void meow() { cout << "Meowing..." << endl; }
};

5. Multiple Inheritance

A derived class inherits from more than one base class.

Example:


class A {
public:
void funcA() { cout << "Function A" << endl; }
};

class B {
public:
void funcB() { cout << "Function B" << endl; }
};

class C : public A, public B {
};

Usage:


C obj;
obj.funcA();
obj.funcB();

6. Hybrid Inheritance

Combination of two or more types of inheritance (single, multiple, multilevel, hierarchical).

Example:

  1. Base class → two derived classes → single derived class from them (mix of multiple + multilevel)

class A { };
class B : public A { };
class C : public A { };
class D : public B, public C { }; // Hybrid

7. Virtual Base Class

Virtual base classes solve ambiguity in multiple inheritance (diamond problem).

Example:


class A {
public:
int x;
};

class B : virtual public A { };
class C : virtual public A { };
class D : public B, public C { };

int main() {
D obj;
obj.x = 10; // No ambiguity
}

Key Points:

  1. Use virtual to ensure only one copy of base class exists
  2. Prevents multiple copies in diamond hierarchy

8. Diamond Problem

Occurs in multiple inheritance when two classes inherit from the same base and a derived class inherits from both.

Without virtual inheritance:


class A { public: int x; };
class B : public A { };
class C : public A { };
class D : public B, public C { };
  1. D will have two copies of A, causing ambiguity.

Solution: Use virtual inheritance as shown above.

Best Practices

  1. Use inheritance for “is-a” relationships
  2. Avoid deep inheritance hierarchies
  3. Prefer composition over inheritance when possible
  4. Use virtual functions carefully to handle polymorphism
  5. Always mark base class destructor as virtual if it will be inherited

Common Mistakes

  1. Forgetting public keyword in inheritance
  2. Mixing types of inheritance without planning
  3. Not using virtual inheritance for diamond problem
  4. Overusing multiple inheritance, making code hard to maintain

Summary

In this chapter, you learned all types of inheritance in C++, including:

  1. Single, multilevel, hierarchical, multiple, and hybrid inheritance
  2. Virtual base classes
  3. Diamond problem and its solution

Inheritance is a core OOP concept that promotes code reuse and establishes relationships between classes, forming the foundation for polymorphism and abstraction.