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.
- A derived class can reuse the data members and member functions of the base class without rewriting the code.
- A derived class can add new members or modify existing functionality according to requirements.
- Inheritance helps reduce code duplication and supports extensibility in C++ programs.
Example: In the following example, Animal is the base class and Dog, Cat and Cow are derived classes that extend the Animal class.

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

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.

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

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

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

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

#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 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.
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.
Situations where Inheritance may not be suitable
- Changes in the base class can affect all derived classes, making the code tightly dependent.
- Flexibility is reduced, as inheritance fixes relationships, while composition allows easier modification.
- Deep or complex class hierarchies make the code harder to understand, maintain, and debug.
- Multiple inheritance can create ambiguity when the same base class is inherited more than once (diamond problem).
- Using virtual functions can introduce a small runtime overhead due to dynamic dispatch.