How Does Inheritance Work in C++?
Learn how inheritance works in C++: base and derived classes, access specifiers, virtual functions, polymorphism, and the diamond problem, with clear examples.
Expected Interview Answer
Inheritance in C++ lets a derived class reuse and extend the data members and member functions of a base class, modelling an 'is-a' relationship so shared behaviour is written once and specialised where needed.
The derived class inherits the base class's public and protected members, and the access specifier used in the derivation (public, protected, or private) controls the accessibility of those inherited members in the derived class. Public inheritance keeps the base interface intact and enables runtime polymorphism through virtual functions and base-class pointers. C++ also supports single, multiple, multilevel, hierarchical, and hybrid inheritance, with virtual base classes solving the diamond ambiguity.
- Reuses existing, tested code instead of duplicating it
- Models real-world is-a relationships clearly
- Enables runtime polymorphism via virtual functions
- Makes code easier to extend and maintain
- Groups shared behaviour in one base class
AI Mentor Explanation
Think of a general Batter class that knows how to hold a stance, judge line and length, and run between wickets. An Opener inherits all of that but adds patience against the new ball, while a Finisher inherits the same base and adds big-hitting. You write the common batting skills once, and each specialised role extends them rather than re-learning how to bat from scratch.
Step-by-Step Explanation
Step 1
Define the base class
Write a class containing the members and functions shared by all related types, marking reusable ones public or protected.
Step 2
Derive a class
Use 'class Derived : public Base' so the derived class inherits the base's public and protected members.
Step 3
Choose the access specifier
Pick public, protected, or private inheritance to control how inherited members are exposed in the derived class.
Step 4
Add or override behaviour
Extend the derived class with new members, and override virtual base functions to specialise behaviour.
Step 5
Use base pointers for polymorphism
Point a Base* at a Derived object and call virtual functions to get runtime dispatch to the derived version.
What Interviewer Expects
- Clear explanation of the is-a relationship
- Knowledge of public, protected, and private inheritance
- How access specifiers affect inherited members
- Awareness of virtual functions and polymorphism
- Understanding of the diamond problem and virtual base classes
Common Mistakes
- Confusing inheritance (is-a) with composition (has-a)
- Assuming private members are directly accessible in the derived class
- Forgetting that constructors and destructors are not inherited
- Not making the base destructor virtual when using polymorphism
Best Answer (HR Friendly)
“Inheritance lets one class build on another, reusing its features instead of rewriting them. It's like a specialised job role that keeps all the general skills of a base role and just adds a few extras on top.”
Code Example
#include <iostream>
#include <string>
using namespace std;
class Animal {
protected:
string name;
public:
Animal(const string& n) : name(n) {}
virtual void speak() const {
cout << name << " makes a sound\n";
}
virtual ~Animal() = default;
};
class Dog : public Animal {
public:
Dog(const string& n) : Animal(n) {}
void speak() const override {
cout << name << " barks\n";
}
};
int main() {
Animal* a = new Dog("Rex");
a->speak(); // Rex barks (runtime polymorphism)
delete a;
}Follow-up Questions
- What is the difference between public, protected, and private inheritance?
- What is the diamond problem and how do virtual base classes solve it?
- Are constructors and destructors inherited in C++?
- How does inheritance enable runtime polymorphism?
- When should you prefer composition over inheritance?
MCQ Practice
1. Which access specifier in the base class makes members accessible in derived classes but not outside?
protected members are accessible within the class and its derived classes, but not from unrelated code.
2. With 'class B : private A', how are A's public members seen in B?
Private inheritance makes all inherited public and protected members private within the derived class.
3. What is required for runtime polymorphism through a base-class pointer?
Calling a virtual function through a base pointer dispatches to the derived override at runtime.
Flash Cards
What relationship does inheritance model? — An is-a relationship, where a derived class is a specialised kind of its base class.
What does public inheritance preserve? — The base class's public interface, keeping public members public in the derived class.
Why make a base destructor virtual? — So deleting a derived object through a base pointer correctly runs the derived destructor.
Are constructors inherited? — No. Constructors and destructors are not inherited, though C++11 allows inheriting constructors with 'using'.