Open In App

Inheritance in C++

Last Updated : 24 Sep, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Inheritance is a fundamental concept in OOP (Object Oriented Programming). It is the mechanism by which one class is allowed to inherit the features (fields and methods) of another class. Inheritance means creating new classes based on existing ones. A class that inherits from another class can reuse the methods and fields of that class.

Example: In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.

inheritance-660x454
C++
#include <iostream>
using namespace std;

class Animal
{
  public:
    void sound()
    {
        cout << "Animal makes a sound" << endl;
    }
};

class Dog : public Animal
{
  public:
    void sound()
    {
        cout << "Dog barks" << endl;
    }
};

class Cat : public Animal
{
  public:
    void sound()
    {
        cout << "Cat meows" << endl;
    }
};

class Cow : public Animal
{
  public:
    void sound()
    {
        cout << "Cow moos" << endl;
    }
};

int main()
{
    Dog d;
    d.sound();

    Cat c;
    c.sound();

    Cow cow;
    cow.sound();

    return 0;
}

Output
Dog barks
Cat meows
Cow moos

Explanation:

  • Animal is the base class with a function sound().
  • Dog, Cat, and Cow are derived classes, each defining their own sound() method.
  • In main(), objects of Dog, Cat, and Cow are created separately.
  • When we call sound() on each object, the respective child class method runs (Dog barks, Cat meows, Cow moos).

Syntax

C++
class ChildClass : public ParentClass
{
    // Additional fields and methods
};

How Inheritance Works in C++?

The colon (:) with an access specifier is used for inheritance in C++. It allows the derived class (child class) to inherit the data members (fields) and member functions (methods) of the base class (parent class).
When a class inherits another class, it gets all the accessible members of the parent class, and the child class can also redefine (override) or add new functionality to them.

Types of Inheritance in C++

inheritance

Below are the different types of inheritance which are supported by C++.

1. Single Inheritance

In single inheritance, a sub-class is derived from only one super class. It inherits the properties and behavior of a single-parent class. Sometimes, it is also known as simple inheritance.

singleinheritance
C++
#include <iostream>
using namespace std;

class Vehicle {
public:
    Vehicle() {
        cout << "This is a Vehicle" << endl;
    }
};

class Car : public Vehicle {
public:
    Car() {
        cout << "This Vehicle is Car" << endl;
    }
};

int main() {
   
    Car obj;
    return 0;
}

Output
This is a Vehicle
This Vehicle is Car

2. Multiple Inheritance

In Multiple inheritance, one class can have more than one superclass and inherit features from all parent classes.

inheritence3
C++
#include <iostream>
using namespace std;

class LandVehicle
{
  public:
    void landInfo()
    {
        cout << "This is a LandVehicle" << endl;
    }
};

class WaterVehicle
{
  public:
    void waterInfo()
    {
        cout << "This is a WaterVehicle" << endl;
    }
};

// Derived class inheriting from both base classes
class AmphibiousVehicle : public LandVehicle, public WaterVehicle
{
  public:
    AmphibiousVehicle()
    {
        cout << "This is an AmphibiousVehicle" << endl;
    }
};

int main()
{
    AmphibiousVehicle obj;

    obj.waterInfo();
    obj.landInfo();

    return 0;
}

Output
This is an AmphibiousVehicle
This is a WaterVehicle
This is a LandVehicle

3. Multilevel Inheritance

Multilevel inheritance in C++ means a class is derived from another derived class, forming a chain of inheritance.

Multilevel-inheritence2
C++
#include <iostream>
using namespace std;

class Vehicle
{
  public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

// Derived class from Vehicle
class FourWheeler : public Vehicle
{
  public:
    FourWheeler()
    {
        cout << "4 Wheeler Vehicles" << endl;
    }
};

// Derived class from FourWheeler
class Car : public FourWheeler
{
  public:
    Car()
    {
        cout << "This 4 Wheeler Vehicle is a Car" << endl;
    }
};

int main()
{
    Car obj;
    return 0;
}

Output
This is a Vehicle
4 Wheeler Vehicles
This 4 Wheeler Vehicle is a Car

4. Hierarchical Inheritance

In hierarchical inheritance, more than one subclass is inherited from a single base class. i.e. more than one derived class is created from a single base class. For example, cars and buses both are vehicle.

Hierarchical-inheritance
C++
#include <iostream>
using namespace std;

class Vehicle
{
  public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

class Car : public Vehicle
{
  public:
    Car()
    {
        cout << "This Vehicle is Car" << endl;
    }
};

class Bus : public Vehicle
{
  public:
    Bus()
    {
        cout << "This Vehicle is Bus" << endl;
    }
};

int main()
{
    Car obj1;
    Bus obj2;
    return 0;
}

Output
This is a Vehicle
This Vehicle is Car
This is a Vehicle
This Vehicle is Bus

5. Hybrid Inheritance

When two or more types of inheritance are combined in one program. For example, a class might use multiple inheritance and also be part of a multilevel inheritance chain.

hybrid-inheritance-
C++
#include <iostream>
using namespace std;

class Vehicle
{
  public:
    Vehicle()
    {
        cout << "This is a Vehicle" << endl;
    }
};

class Fare
{
  public:
    Fare()
    {
        cout << "Fare of Vehicle" << endl;
    }
};

class Car : public Vehicle
{
  public:
    Car()
    {
        cout << "This Vehical is a Car" << endl;
    }
};

class Bus : public Vehicle, public Fare
{
  public:
    Bus()
    {
        cout << "This Vehicle is a Bus with Fare";
    }
};

int main()
{
    Bus obj2;
    return 0;
}

Output
This is a Vehicle
Fare of Vehicle
This Vehicle is a Bus with Fare

Hybrid Inheritance can lead to the diamond problem in C++. This happens when a class inherits from two classes that both share the same base class. As a result the derived class gets multiple copies of the base class members, which creates ambiguity about which one to use.

Note : The solution is to use virtual inheritance, so only a single copy of the base class is shared.

Advantages of Inheritance in C++

  • Code Reusability : Derived class can directly reuse data members and methods of its base class, avoiding code duplication.
  • Abstraction : Supports abstract classes (classes with pure virtual functions) that define a common interface, enforcing abstraction.
  • Class Hierarchy : You can build hierarchies (base → derived → further derived) to model real-world relationships.
  • Polymorphism : Fully supports runtime polymorphism through virtual functions, and also compile-time polymorphism via function overloading and templates.

Disadvantages of Inheritance in C++

  • Tight Coupling : The child class becomes dependent on the parent class. Any change in the base class may force changes in derived classes.
  • Reduced Flexibility : Sometimes inheritance is misused where composition (has-a relationship) would be better, leading to less flexible code.
  • Increased Complexity : Deep inheritance hierarchies (multilevel or hybrid) can make the code hard to understand, maintain, and debug.
  • Diamond Problem : With hybrid or multiple inheritance, ambiguity can occur if the same base class is inherited multiple times.
  • Overhead of Virtual Functions : If polymorphism (virtual functions) is used, there’s a small runtime overhead due to virtual table lookups.

Inheritance | Video
Visit Course explore course icon
Article Tags :

Explore