Friend Functions

What Are the 5 Types of Inheritance in C++?

Learn the five commonly listed types of inheritance in C++: single, multiple, multilevel, hierarchical and hybrid, with a short example of each.

On this page
  1. The short answer
  2. The five types at a glance
  3. Single inheritance
  4. Multiple inheritance
  5. Multilevel inheritance
  6. Hierarchical inheritance
  7. Hybrid inheritance and the diamond problem
  8. Inheritance and polymorphism
  9. What the public keyword means in these examples
  10. Summary

The short answer

Textbooks usually list five types of inheritance in C++: single, multiple, multilevel, hierarchical and hybrid. These names come from general object-oriented programming terms rather than from the C++ standard, but they describe the common shapes that class hierarchies take.

If the basic idea of a derived class is new, read the basics of inheritance in c first. Single inheritance is the simplest shape. The others add more base classes, more derived classes, or more levels.

The five types at a glance

Type Shape Example
Single One base class, one derived class Dog derives from Animal
Multiple One derived class with two or more direct base classes Duck derives from Flyer and Swimmer
Multilevel A chain, where each class derives from the one above it SportsCar derives from Car, which derives from Vehicle
Hierarchical One base class with several derived classes Circle and Square both derive from Shape
Hybrid A mix of two or more of the other types Combined derives from Left and Right, which both derive from Base

Single inheritance

In single inheritance, one derived class inherits from one base class. Here is a complete program:

#include <iostream>

class Animal {
public:
    void eat() const { std::cout << "Eating\n"; }
};

class Dog : public Animal {
public:
    void bark() const { std::cout << "Woof\n"; }
};

int main() {
    Dog dog;
    dog.eat();   // inherited from Animal
    dog.bark();
}

This program prints Eating and then Woof, each on its own line.

Multiple inheritance

A derived class can have two or more direct base classes. The base classes are listed after the colon, separated by commas:

class Flyer {};
class Swimmer {};

class Duck : public Flyer, public Swimmer {};

Multiple inheritance can suit a class that really combines two separate roles. It also adds complexity, so many C++ designs use it sparingly. The examples in this section leave out member functions to keep the shapes clear.

Multilevel inheritance

In multilevel inheritance, the classes form a chain, and each class derives from the one above it:

class Vehicle {};
class Car : public Vehicle {};
class SportsCar : public Car {};

SportsCar gets the members of Car, and Car gets its members from Vehicle. That means SportsCar inherits from Vehicle indirectly.

Hierarchical inheritance

In hierarchical inheritance, several derived classes share one base class:

class Shape {};
class Circle : public Shape {};
class Square : public Shape {};

Each derived class gets the base class’s members and can add its own.

Hybrid inheritance and the diamond problem

Hybrid inheritance mixes two or more of the other types. The most discussed form has four classes in a diamond shape: a base class, two classes derived from it, and a fourth class derived from both of those:

class Base {};
class Left : public Base {};
class Right : public Base {};
class Combined : public Left, public Right {};

In this layout, Combined ends up with two separate Base subobjects, one through Left and one through Right. If Base has a data member named value, then c.value is ambiguous, and the code won’t compile.

A common fix is virtual inheritance, which makes both paths share one Base:

class Base {
public:
    int value = 0;
};

class Left : public virtual Base {};
class Right : public virtual Base {};
class Combined : public Left, public Right {};

int main() {
    Combined c;
    c.value = 5;  // compiles: only one Base subobject exists
}

Virtual inheritance is an advanced topic. This is only an outline of why it exists.

Inheritance and polymorphism

Readers sometimes ask, What is the difference between polymorphism and inheritance? Inheritance lets one class reuse and extend another class’s members. Polymorphism lets code treat objects of different types through a common interface, so the right function runs for each type. The two often work together, since polymorphism in c often relies on inheritance and virtual functions.

What the public keyword means in these examples

In class Dog : public Animal, the word public is an inheritance access specifier. With public inheritance, the public members of Animal stay public in Dog. Protected and private inheritance also exist, and they change how inherited members are exposed. This is separate from the access modifiers in c used inside a class to control who can see each member.

Summary

C++ inheritance is commonly described with five types: single, multiple, multilevel, hierarchical and hybrid. Single inheritance is the simplest place to start. Add more base classes or deeper chains only when the design calls for them, since each extra layer makes the code harder to follow.

The complete guide to friend functions

Friend Functions in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.