Introduction
Inheritance is a fundamental concept in object-oriented programming (OOP), and C++ fully supports this feature. It allows a class (called the derived or child class) to inherit properties and behaviors from another class (called the base or parent class). This concept promotes code reusability, maintainability, and scalability, making it easier to build complex applications with less redundancy.

In this blog, we'll explore the types of inheritance in C++, how they work, their syntax, and practical examples. We'll also touch upon where inheritance is useful in real-world scenarios, and how it’s asked in interviews.
Inheritance is a mechanism through which one class acquires the properties (data members) and functionalities (member functions) of another class. It helps in building hierarchical relationships between classes.
Key Concepts:
Copy Code
class BaseClass {
// base class members
};
class DerivedClass : accessSpecifier BaseClass {
// derived class members
};Example:
Copy Code
#include <iostream>
using namespace std;
class Animal {
public:
void speak() {
cout << "Animal speaks" << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Dog barks" << endl;
}
};
int main() {
Dog myDog;
myDog.speak(); // Inherited function
myDog.bark(); // Dog’s own function
return 0;
}C++ supports the following types of inheritance:
1. Single Inheritance
A single derived class inherits from a single base class.
Copy Code
class A {
public:
void display() {
cout << "Class A" << endl;
}
};
class B : public A {
};2. Multiple Inheritance
A derived class inherits from more than one base class.
Copy Code
class A {
public:
void showA() {
cout << "Class A" << endl;
}
};
class B {
public:
void showB() {
cout << "Class B" << endl;
}
};
class C : public A, public B {
};3. Multilevel Inheritance
A class is derived from a derived class.
Copy Code
class A {
public:
void display() {
cout << "Class A" << endl;
}
};
class B : public A {
};
class C : public B {
};4. Hierarchical Inheritance
Multiple derived classes inherit from a single base class.
Copy Code
class A {
public:
void display() {
cout << "Base class A" << endl;
}
};
class B : public A {
};
class C : public A {
};5. Hybrid Inheritance
Combination of two or more types of inheritance.
Copy Code
class A {
public:
void displayA() {
cout << "Class A" << endl;
}
};
class B : public A {
};
class C {
public:
void displayC() {
cout << "Class C" << endl;
}
};
class D : public B, public C {
};The access level of inherited members depends on the access specifier used:
| Access Specifier | Public Members | Protected Members | Private Members |
| Public | Public | Protected | Not Inherited |
| Protected | Protected | Protected | Not Inherited |
| Private | Private | Private | Not Inherited |
Constructors of base classes are called before derived class constructors. Destructor order is the reverse.
Copy Code
class A {
public:
A() {
cout << "Constructor of A" << endl;
}
~A() {
cout << "Destructor of A" << endl;
}
};
class B : public A {
public:
B() {
cout << "Constructor of B" << endl;
}
~B() {
cout << "Destructor of B" << endl;
}
};Function overriding allows a derived class to provide a specific implementation of a method defined in the base class.
Copy Code
class Base {
public:
void show() {
cout << "Base class show()" << endl;
}
};
class Derived : public Base {
public:
void show() {
cout << "Derived class show()" << endl;
}
};Let’s say you’re building a vehicle system.
Copy Code
class Vehicle {
public:
void start() {
cout << "Starting vehicle" << endl;
}
};
class Car : public Vehicle {
public:
void drive() {
cout << "Driving car" << endl;
}
};1. What is inheritance in C++?
2. Explain types of inheritance with examples.
3. What happens when constructors and destructors are involved?
4. How does access specifier affect inheritance?
5. What is function overriding?
6. How is multiple inheritance resolved in C++?
Want to explore C++ concepts deeper? Join Uncodemy's C++ Programming Course and start your journey with expert-led tutorials:
C++ Programming Course on Uncodemy
Inheritance in C++ is a cornerstone of object-oriented design. It promotes reusable, maintainable, and scalable software architecture. By understanding the types, syntax, and nuances of inheritance, you can write better and more professional C++ code.
Prepare well with theory, implement examples, and practice problems to gain mastery whether for projects, interviews, or real-world applications.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR