Published: 17 Nov 2025 | Reading Time: 7 min read
Inheritance is one of the core principles of object-oriented programming (OOP), and through inheritance, a class (derived class) inherits the attributes and behaviour (methods) of another class (base class). The inheritance in C++ enables code reuse and develops an association between classes such that classes can share common characteristics and functionalities.
This article discusses the definition, types, syntax, examples, advantages, disadvantages, and real-world implementation of inheritance in C++. Let us explore the key features of inheritance and its usage in C++.
Inheritance is a system where the members and methods of a base class are inherited into a derived class. This supports code reuse and helps you maintain a more structured hierarchy in your code. The derived class may have other additional features of its own or override the ones inherited.
The inheritance syntax in c++ is defined below:
class DerivedClass : accessSpecifier BaseClass {
// Derived class members
};
Syntax Components:
Now we know about inheritance and private members, let's understand how to implement inheritance in C++ through examples.
Access specifiers control the accessibility of class members to other classes. The three primary access specifiers are:
Private members of the base class are not directly accessible by default to the derived class. Private members may be made accessible through the following ways:
By declaring members safe in the base class, they can be inherited by the derived class.
Example: Protected Inheritance
#include <iostream>
class Base {
protected:
int x;
public:
Base() : x(5) {}
};
class Derived : public Base {
public:
void display() {
std::cout << "Value of x: " << x << std::endl; // x is inherited and accessible
}
};
int main() {
Derived obj;
obj.display(); // Calling the display function
return 0;
}
Explanation:
Output:
Value of x: 5
Alternatively, you can supply getter/setter methods through which private members are accessed in the derived class.
Example: Public or Protected Inheritance
#include <iostream>
class Base {
private:
int x;
public:
Base() : x(10) {}
int getX() { return x; } // Accessor method
};
class Derived : public Base {
public:
void display() {
std::cout << "Value of x: " << getX() << std::endl; // Using accessor method
}
};
int main() {
Derived obj;
obj.display(); // Calling the display function
return 0;
}
Explanation:
Output:
Value of x: 10
Protected members play a crucial role in C++ inheritance by balancing data encapsulation with code reusability. The protected access specifier allows certain class members to be accessible within the base class and all derived classes, but not from outside the class hierarchy.
A protected member is a class data member or method declared with the protected access specifier. Protected members are not accessible from outside the class, but they are accessible to:
This makes protected members ideal for scenarios where you want derived classes to have direct access to certain data or functionality, while still keeping those details hidden from other parts of the program.
#include <iostream>
using namespace std;
class Base {
protected:
int value;
public:
void setProtectedMember(int v) {
value = v;
}
int getProtectedMember() {
return value;
}
};
class Derived : public Base {
public:
void showValue() {
cout << "Accessing protected member in derived class: " << value << endl;
}
};
int main() {
Derived obj;
obj.setProtectedMember(42); // Using public setter from base class
obj.showValue(); // Accesses protected member directly in derived class
cout << "Getting protected member via public getter: "
<< obj.getProtectedMember() << endl;
return 0;
}
Explanation:
Friend functions can access protected members of a class, providing flexibility when external functions need to interact with internal class data.
C++ supports several kinds of inheritance. Let us review the most inheritance types in c++ with example programs:
Single inheritance is where a derived class is inherited from one base class and supports reuse and extension of the functions.
Syntax:
class DerivedClass : public BaseClass
{ ... };
Inheritance Structure:
Animal (base class)
|
Dog (derived class)
C++ Code Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking." << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
return 0;
}
Explanation:
Output:
Eating food.
Barking.
Multiple inheritance is when the derived class is inherited by more than one base class and gains access to more than one class member.
Syntax:
class DerivedClass : public BaseClass1, public BaseClass2
{ ... };
Inheritance Structure:
Person
/ \
Car Vehicle
C++ Code Example:
#include <iostream>
using namespace std;
class Person{
public:
void eat() {
cout << "Eating food." << endl;
}
};
class Vehicle {
public:
void drive() {
cout << "Driving vehicle." << endl;
}
};
class Car : public Person, public Vehicle {
public:
void honk() {
cout << "Honking." << endl;
}
};
int main() {
Car c;
c.eat(); // Inherited from Person
c.drive(); // Inherited from Vehicle
c.honk(); // Defined in Car
return 0;
}
Explanation:
Output:
Eating food.
Driving vehicle.
Honking.
Hierarchical inheritance in c++ is where more than one derived class inherits from a single base class, with common attributes and methods.
Syntax:
class DerivedClass1 : public BaseClass
{ ... };
class DerivedClass2 : public BaseClass
{ ... };
Inheritance Structure:
Animal
/ \
Dog Cat
C++ Code Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking." << endl;
}
};
class Cat : public Animal {
public:
void meow() {
cout << "Meowing." << endl;
}
};
int main() {
Dog d;
Cat c;
d.eat(); // Inherited from Animal
d.bark(); // Defined in Dog
c.eat(); // Inherited from Animal
c.meow(); // Defined in Cat
return 0;
}
Explanation:
Output:
Eating food.
Barking.
Eating food.
Meowing.
Multilevel or multi inheritance in c++ is defined when a class inherits from a derived class, creating an inheritance chain.
Syntax:
class DerivedClass1 : public BaseClass { ... };
class DerivedClass2 : public DerivedClass1 { ... };
Inheritance Structure:
Animal
/ \
Dog Puppy
C++ Code Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking." << endl;
}
};
class Puppy : public Dog {
public:
void play() {
cout << "Playing." << endl;
}
};
int main() {
Puppy p;
p.eat(); // Inherited from Animal
p.bark(); // Inherited from Dog
p.play(); // Defined in Puppy
return 0;
}
Explanation:
Output:
Eating food.
Barking.
Playing.
Hybrid inheritance in c++ is a combination of two or more inheritances like multilevel and multiple inheritance in a program.
Syntax:
class DerivedClass1 : public BaseClass { ... };
class DerivedClass2 : public DerivedClass1, public AnotherBaseClass { ... };
Inheritance Structure:
Animal
/ \
Car FlyingCar
C++ Code Example:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating food." << endl;
}
};
class Vehicle {
public:
void drive() {
cout << "Driving vehicle." << endl;
}
};
class Car : public Animal {
public:
void honk() {
cout << "Honking." << endl;
}
};
class FlyingCar : public Car, public Vehicle {
public:
void fly() {
cout << "Flying." << endl;
}
};
int main() {
FlyingCar fc;
fc.eat(); // Inherited from Animal
fc.drive(); // Inherited from Vehicle
fc.honk(); // Inherited from Car
fc.fly(); // Defined in FlyingCar
return 0;
}
Explanation:
Output:
Eating food.
Driving vehicle.
Honking.
Flying.
Function overriding (also called method overriding) allows a derived class to provide its own implementation of a member function that is already defined in its base class. This is a key feature of object-oriented programming in C++, supporting code reusability and enabling polymorphism.
When a derived class defines a member function with the same name, return type, and parameters as a member function in its base class, the derived class's version overrides the base class version. This means that when the function is called on an object of the derived class, the derived class's implementation is executed—even if the object is referenced through a base class pointer or reference (when using virtual functions).
#include <iostream>
using namespace std;
class Base {
public:
void display() {
cout << "Display from Base class" << endl;
}
};
class Derived : public Base {
public:
void display() {
cout << "Display from Derived class" << endl;
}
};
int main() {
Derived obj;
obj.display(); // Calls Derived's display()
obj.Base::display(); // Calls Base's display()
return 0;
}
Explanation:
In multiple inheritance, ambiguity can occur if two base classes have member functions with the same signature. The derived class must specify which base class's function to use, often with the scope resolution operator, to avoid ambiguity.
Private members (data members or methods) of the base class are not accessible directly in the derived class, so they cannot be overridden. Overriding applies only to accessible (usually public or protected) member functions.
When overriding functions in derived classes, changes to the base class's member functions can lead to unexpected behavior or bugs in the derived class, a situation known as the fragile base class problem. Hidden dependencies between base and derived classes can make maintenance more difficult, especially in complex inheritance hierarchies.
Here are the advantages of inheritance in C++:
Code Reusability: Inheritance enables a derived class to inherit the properties and methods of a base class, avoiding duplication and encouraging efficient coding.
Easier Maintenance: Inherited classes share similar functionalities; hence, modifying and maintaining the code in one place (the base class) is easier.
Extensibility: New classes or functionality can be added to an existing class structure with minimal modification, allowing scalable and adaptable code.
Logical Structure: It encourages a class hierarchy, making the program structure logical and easy to maintain.
Simplified Debugging: Because standard functionality is taken care of in the base class, bugs in common functions must be corrected only once, thus making debugging easier.
Here are the disadvantages of inheritance in C++:
Increased Complexity: Overusing inheritance can lead to a complex class structure, making the code harder to understand and maintain.
Tight Coupling: Derived classes are closely coupled with the base classes, and changes in the base class can necessitate changes in derived classes.
Unintended Side Effects: Changes in the base class might unintentionally influence the derived classes, resulting in bugs.
Ambiguity Issues: When multiple inheritance is applied, there can be uncertainty in accessing members of two or more base classes, leading to confusing code and bugs.
Performance Overhead: Inheritance may introduce increased overhead, particularly when the methods of base classes are repeatedly overridden in derived classes, which can impact performance.
Quick Note: Inheritance is a powerful tool in C++ for code reuse and structure, but it should be used thoughtfully. Overusing it can make programs complex and more challenging to maintain. Keep hierarchies simple for best results.
In conclusion, C++ inheritance is an effective means of ensuring code reuse, modularity, and maintainability by allowing derived classes to inherit characteristics and behaviours from base classes. It provides more formal and structured programming, simplifying existing functionality. However, excessive use of inheritance leads to greater complexity, tight coupling, and some maintainability problems, particularly in multi-inheritance or deep inheritance. Thus, although inheritance is an important feature in object-oriented programming, care must be taken with code reuse and program simplicity.
Inheritance in C++ is crucial for building reusable, organized, and scalable code. It enables developers to extend existing functionality without rewriting code, fosters a clear class hierarchy, and supports object-oriented principles like polymorphism and encapsulation, making programs easier to maintain, debug, and enhance over time.
The is-a relationship refers to the connection where a derived class is a specialized form of its base class. For example, if Dog is derived from Animal, then a Dog is an Animal. This relationship is the foundation of simple inheritance in C++ and helps structure code logically.
C++ supports several types of inheritance:
Access specifiers—public inheritance, protected inheritance, and private inheritance—determine how base class members are accessible in the derived class.
Public inheritance is when a derived class publicly inherits from a base class (class Derived : public Base). This means the derived class is a specialized form of the base class, supporting the is-a relationship. Public members of the base class remain public in the derived class, and protected members remain protected.
Protected inheritance (class Derived : protected Base) means that public and protected members of the base class become protected in the derived class. This limits the accessibility of inherited members to the derived class and its subclasses, but not to outside code.
Private inheritance (class Derived : private Base) means that public and protected members of the base class become private in the derived class. This form of inheritance does not establish an is-a relationship, but rather an implemented-in-terms-of relationship, often used for code reuse without exposing the base class interface.
Virtual inheritance is a technique used to prevent multiple "instances" of a base class when using multiple inheritance, particularly to resolve the diamond problem. By declaring a base class as virtual (class Derived : virtual public Base), C++ ensures that only one instance of the base class's members is inherited, avoiding ambiguity.
Simple inheritance is another term for single inheritance, where one derived class inherits from a single base class. It is the most straightforward and commonly used form of inheritance in C++.
Simple inheritance, also called single inheritance, means a single derived class inherits from a single base class. It is the most straightforward way to reuse code and establish an is-a relationship.
Linked List in C++: Types, Operations, and Examples - Learn linked list in C++ with clear explanations, types, operations, and examples to build a strong foundation in data structures. (07 Jan 2026, 5 min read)
Merge Two Sorted Arrays with Examples - Learn how to merge two sorted arrays efficiently. Explore different approaches, time complexity, examples, and real-world use cases. (06 Jan 2026, 5 min read)
Access Modifiers in C++: public, private & protected Explained - Learn about access modifiers in C++: public, private, and protected. Understand how they control access to class members with simple examples. (02 Jan 2026, 5 min read)
Virtual Base Class in C++: Explained with Examples - Learn what a Virtual Base Class in C++ is, its use in multiple inheritance, and how it helps avoid ambiguity with clear examples and explanations. (02 Jan 2026, 8 min read)
Top C++ MCQs for Freshers and Experienced Candidates - Practice top C++ MCQ questions with answers. Perfect for competitive exams, technical interviews, and improving your C++ programming skills. (02 Jan 2026, 8 min read)
Sort in C++: Algorithms, Methods & Code Examples - Discover how to sort in C++ using popular algorithms such as bubble sort, selection sort, and others. Explore code examples for effective sorting techniques. (02 Jan 2026, 5 min read)
Source: NxtWave - https://www.ccbp.in/blog/articles/inheritance-in-cpp