Inheritance in C++ (original) (raw)

Last Updated : 15 Jun, 2026

Inheritance is a core Object-Oriented Programming (OOP) concept that allows one class to inherit the properties and behaviors of another class. It helps create a new class from an existing class, improving code reusability and hierarchical organization of classes.

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

animal_class

Illustration for Inheritance

C++ `

#include 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:

Syntax

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

How Inheritance Works in C++?

Inheritance in C++ allows a class to acquire the properties and behaviors of another class using the colon (:) operator. It helps create a relationship between classes, enabling code reuse and easier maintenance.

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.

inheritence

Single Inheritance

C++ `

#include 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.

Multiple_inheritance

Multiple Inheritance

C++ `

#include 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_inheritance

Multilevel Inheritance

C++ `

#include 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

Hierarchical Inheritance

C++ `

#include 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.

inheritence_5

Hybrid Inheritance

C++ `

#include 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 Vehicle 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.

Virtual inheritance is used to solve the diamond problem in C++. It ensures that when a class inherits the same base class through multiple inheritance paths, only one shared copy of the base class is inherited, avoiding ambiguity.

Advantages of Inheritance in C++

Situations where Inheritance may not be suitable