Inheritance
Inheritance is one of the four fundamental pillars of object-oriented programming, enabling code reuse and creating hierarchical relationships between classes. It allows you to create new classes based on existing ones, inheriting their attributes and methods while adding new functionality or modifying existing behavior. This powerful mechanism reduces code duplication, improves maintainability, and enables the creation of more organized, scalable software systems.
Introduction to Inheritance
Inheritance is a mechanism where a new class (derived class) is created from an existing class (base class). The derived class inherits all the properties and behaviors of the base class, promoting code reusability. This relationship is often described as an "is-a" relationship - for example, a Dog "is-a" Animal, or a Car "is-a" Vehicle.
The base class (also called parent class or superclass) contains common features shared by multiple classes. The derived class (also called child class or subclass) inherits these features and can add its own unique characteristics. This creates a natural hierarchy that models real-world relationships and makes code more intuitive and maintainable.
Inheritance enables polymorphism, where objects of different derived classes can be treated uniformly through their common base class. This is essential for building flexible, extensible software systems that can grow and adapt to changing requirements without major restructuring of existing code.
Syntax
class BaseClass {
// base class members
};
class DerivedClass : access_specifier BaseClass {
// derived class members
};Types of Inheritance
1. Single Inheritance
A derived class inherits from only one base class:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Dog : public Animal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog d;
d.eat(); // Inherited from Animal
d.bark(); // Own method
return 0;
}2. Multiple Inheritance
A derived class inherits from multiple base classes:
#include <iostream>
using namespace std;
class A {
public:
void displayA() {
cout << "Class A" << endl;
}
};
class B {
public:
void displayB() {
cout << "Class B" << endl;
}
};
class C : public A, public B {
public:
void displayC() {
cout << "Class C" << endl;
}
};
int main() {
C obj;
obj.displayA();
obj.displayB();
obj.displayC();
return 0;
}3. Multilevel Inheritance
A class is derived from a derived class, creating a chain:
#include <iostream>
using namespace std;
class Animal {
public:
void eat() {
cout << "Eating..." << endl;
}
};
class Mammal : public Animal {
public:
void breathe() {
cout << "Breathing..." << endl;
}
};
class Dog : public Mammal {
public:
void bark() {
cout << "Barking..." << endl;
}
};
int main() {
Dog d;
d.eat(); // From Animal
d.breathe(); // From Mammal
d.bark(); // Own method
return 0;
}4. Hierarchical Inheritance
Multiple derived classes inherit from a single base class:
#include <iostream>
using namespace std;
class Shape {
public:
void display() {
cout << "This is a shape" << endl;
}
};
class Circle : public Shape {
public:
void drawCircle() {
cout << "Drawing circle" << endl;
}
};
class Rectangle : public Shape {
public:
void drawRectangle() {
cout << "Drawing rectangle" << endl;
}
};
int main() {
Circle c;
Rectangle r;
c.display();
c.drawCircle();
r.display();
r.drawRectangle();
return 0;
}5. Hybrid Inheritance
Combination of multiple types of inheritance:
#include <iostream>
using namespace std;
class A {
public:
void displayA() {
cout << "Class A" << endl;
}
};
class B : public A {
public:
void displayB() {
cout << "Class B" << endl;
}
};
class C : public A {
public:
void displayC() {
cout << "Class C" << endl;
}
};
class D : public B, public C {
public:
void displayD() {
cout << "Class D" << endl;
}
};
int main() {
D obj;
// obj.displayA(); // Ambiguous - which A?
obj.B::displayA(); // Specify which A
obj.displayB();
obj.displayC();
obj.displayD();
return 0;
}Access Specifiers in Inheritance
| Base Class Access | public | protected | private |
|---|---|---|---|
| public inheritance | public | protected | Not accessible |
| protected inheritance | protected | protected | Not accessible |
| private inheritance | private | private | Not accessible |
Function Overriding
Derived class can override base class functions:
#include <iostream>
using namespace std;
class Base {
public:
void display() {
cout << "Base class display" << endl;
}
};
class Derived : public Base {
public:
void display() { // Overriding
cout << "Derived class display" << endl;
}
};
int main() {
Derived d;
d.display(); // Calls Derived::display()
d.Base::display(); // Calls Base::display()
return 0;
}Constructor and Destructor in Inheritance
Base class constructor is called before derived class constructor. Destructors are called in reverse order:
#include <iostream>
using namespace std;
class Base {
public:
Base() {
cout << "Base constructor" << endl;
}
~Base() {
cout << "Base destructor" << endl;
}
};
class Derived : public Base {
public:
Derived() {
cout << "Derived constructor" << endl;
}
~Derived() {
cout << "Derived destructor" << endl;
}
};
int main() {
Derived d;
// Output:
// Base constructor
// Derived constructor
// Derived destructor
// Base destructor
return 0;
}Summary
| Topic | Key Points | Difficulty |
|---|---|---|
| Single Inheritance | One base class, one derived class, simplest form | Beginner |
| Multiple Inheritance | Derived class inherits from multiple base classes, can cause ambiguity | Advanced |
| Multilevel Inheritance | Chain of inheritance, A→B→C, creates inheritance hierarchy | Intermediate |
| Hierarchical Inheritance | Multiple derived classes from one base, common base shared | Intermediate |
| Hybrid Inheritance | Combination of multiple types, may require virtual inheritance | Advanced |
| Access Specifiers | public/protected/private inheritance control member visibility | Intermediate |
| Constructor/Destructor Order | Base constructor first, derived last; destructors reverse order | Intermediate |
Frequently Asked Questions
Q1: What is the difference between public, protected, and private inheritance?
Public inheritance: base class public members become public in derived, protected become protected. Protected inheritance: both public and protected become protected. Private inheritance: both become private. Public inheritance is most common (is-a relationship), private inheritance is rarely used (implementation inheritance).
Q2: What is the diamond problem in multiple inheritance?
Diamond problem occurs when a class inherits from two classes that both inherit from the same base class, creating ambiguity. For example, D inherits from B and C, both of which inherit from A. D would have two copies of A's members. Solution: use virtual inheritance (virtual keyword in inheritance declaration) to ensure only one copy of the base class.
Q3: Can a derived class access private members of the base class?
No, private members of the base class are not accessible in the derived class, regardless of inheritance type. They remain private to the base class. To allow derived classes to access members while keeping them hidden from external code, use protected access specifier instead of private.
Q4: What is function overriding and how is it different from overloading?
Function overriding occurs when a derived class defines a function with the same signature as a base class function, replacing the base implementation. Overloading is having multiple functions with the same name but different signatures in the same scope. Overriding requires inheritance, overloading doesn't. Use virtual keyword for runtime polymorphism with overriding.
Q5: Why are base class constructors called before derived class constructors?
Base class must be fully constructed before derived class can be constructed, because derived class may depend on base class members. This ensures base class is in a valid state before derived class initialization. Destructors are called in reverse order (derived first, then base) to ensure proper cleanup.
Q6: Can I prevent a class from being inherited?
In C++11+, you can use the final keyword: class Base final ;prevents any class from inheriting from Base. You can also mark specific functions as final to prevent overriding. This is useful for ensuring certain classes or methods cannot be modified through inheritance.
Q7: What is the difference between inheritance and composition?
Inheritance represents "is-a" relationship (Dog is-a Animal), while composition represents "has-a" relationship (Car has-a Engine). Inheritance: derived class is a type of base class. Composition: class contains another class as a member. Prefer composition when you need functionality, inheritance when you need polymorphism. Composition is more flexible and less coupled.
Q8: How do I call a base class function from a derived class?
Use scope resolution operator: BaseClass::functionName(). For example, if Derived overrides a function but wants to call the base version: Base::display(). This is useful when overriding functions but still needing the base class functionality, or when resolving ambiguity in multiple inheritance.
Conclusion
Inheritance is a fundamental OOP concept that enables code reuse, hierarchical organization, and polymorphism. Understanding different types of inheritance helps you design better class hierarchies that model real-world relationships accurately. Single and hierarchical inheritance are most common, while multiple inheritance requires careful design to avoid ambiguity.
Access specifiers in inheritance control how base class members are accessible in derived classes, providing fine-grained control over encapsulation. Constructor and destructor order ensures proper initialization and cleanup, with base classes always constructed first and destroyed last.
Mastery of inheritance is essential for building complex, maintainable C++ applications. Combined with polymorphism, inheritance enables powerful design patterns and flexible software architectures. As you progress, you'll learn to balance inheritance with composition, choosing the right relationship type for each design situation.
Related Links
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026