Skip to main content
C++ Fundamentals for Beginners to Advanced
CHAPTER 17 Beginner

Inheritance in C++

Updated: May 17, 2026
5 min read

# CHAPTER 17

Inheritance in C++

1. Introduction

If you are coding a game, a Player and an Enemy both need health, speed, and an x,y position. Instead of writing that same code twice, you can create a base class called Entity, and have both Player and Enemy inherit from it. Inheritance is the mechanism that allows one class to acquire the properties and methods of another.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Define Base (Parent) and Derived (Child) classes.
  • Use the protected access modifier.
  • Understand Single, Multilevel, and Multiple Inheritance.
  • See how Constructors behave during inheritance.

3. Syntax of Inheritance

Syntax: class ChildClass : access_modifier ParentClass { };
cpp
1234567891011121314151617181920212223242526
#include <iostream>
using namespace std;

// Base Class (Parent)
class Animal {
  public:
    void eat() {
        cout << "I can eat!" << endl;
    }
};

// Derived Class (Child)
class Dog : public Animal {
  public:
    void bark() {
        cout << "Woof woof!" << endl;
    }
};

int main() {
    Dog myDog;
    myDog.bark(); // Its own method
    myDog.eat();  // Inherited method from Animal!
    
    return 0;
}

4. The protected Access Modifier

We know private hides data completely, and public exposes it completely. What if you want to hide data from main(), but still allow Child classes to access it? You use protected.
ModifierAccess from own ClassAccess from Derived ClassAccess from main()
publicYesYesYes
protectedYesYesNo
privateYesNoNo
cpp
1234567891011
class Entity {
  protected:
    int health; // Hidden from main(), but visible to children
};

class Player : public Entity {
  public:
    void takeDamage(int dmg) {
        health -= dmg; // Allowed! Because health is protected.
    }
};

5. Types of Inheritance

1. Single Inheritance: One child inherits from one parent. (e.g., Dog inherits from Animal).

2. Multilevel Inheritance: A child inherits from a parent, and then another child inherits from that child.

cpp
123
class Vehicle {};
class Car : public Vehicle {};
class SportsCar : public Car {}; // Inherits from Car and Vehicle

3. Multiple Inheritance: A child inherits from *more than one* parent simultaneously. (C++ supports this, but Java/C# do not because it can get messy!).

cpp
1234567891011121314151617
class Printer {
  public: void print() { cout << "Printing..." << endl; }
};

class Scanner {
  public: void scan() { cout << "Scanning..." << endl; }
};

// Inherits from TWO parents!
class MultiFunctionPrinter : public Printer, public Scanner {}; 

int main() {
    MultiFunctionPrinter mfp;
    mfp.print();
    mfp.scan();
    return 0;
}

6. Constructors and Inheritance

When you create a child object, the Parent's constructor is called first, followed by the Child's constructor. When destroyed, the order is reversed: Child destructor first, then Parent destructor.
cpp
12345678910111213141516
class Base {
  public:
    Base() { cout << "Base created\n"; }
    ~Base() { cout << "Base destroyed\n"; }
};

class Derived : public Base {
  public:
    Derived() { cout << "Derived created\n"; }
    ~Derived() { cout << "Derived destroyed\n"; }
};

int main() {
    Derived d;
    return 0;
}

Output:

text
1234
Base created
Derived created
Derived destroyed
Base destroyed

7. Calling a Parameterized Base Constructor

If the Base class does not have a default constructor, the Derived class MUST explicitly call the Base's parameterized constructor using an initializer list.
cpp
123456789101112
class Animal {
  public:
    Animal(int age) { /*...*/ }
};

class Dog : public Animal {
  public:
    // Calling the Base constructor in the initializer list
    Dog(int age) : Animal(age) {
        cout << "Dog created";
    }
};

8. Memory-Level Explanation

When a Dog object is instantiated, the OS allocates a single block of memory large enough to hold all the data members of Animal PLUS all the data members of Dog. The base class data sits at the top of this memory layout, followed by the derived class data.

9. Common Mistakes

  • Private Inheritance by Accident: If you forget public in class Dog : Animal, it defaults to private inheritance! This means eat() becomes private in Dog and cannot be called from main(). Always write class Dog : public Animal.
  • The Diamond Problem: In multiple inheritance, if B and C inherit from A, and D inherits from both B and C, D gets two copies of A's data! (Solved using virtual inheritance).

10. Exercises

  1. 1. Create a base class Shape with protected width and height. Create a derived class Rectangle with a getArea() method.
  1. 2. Demonstrate Multilevel inheritance by creating Grandparent, Parent, and Child classes.

11. MCQ Quiz with Answers

Question 1

What is the main advantage of Inheritance?

Question 2

Which symbol is used to define inheritance?

Question 3

Which access modifier hides data from main() but allows Child classes to access it?

Question 4

What is it called when a class inherits from two or more Base classes?

Question 5

When a child object is created, whose constructor runs first?

Question 6

When a child object is destroyed, whose destructor runs first?

Question 7

If you omit the access modifier in class Child : Parent, what is the default inheritance type?

Q8. Can a derived class directly access private members of its base class? a) Yes b) No Answer: b) No (Only public and protected)
Question 9

What is Multilevel Inheritance?

Question 10

How do you call a specific Parameterized constructor of the Base class from the Derived class?

12. Interview Questions

  • Q: Explain the "Diamond Problem" in Multiple Inheritance and how to solve it.
  • Q: What is the difference between public, protected, and private inheritance?
  • Q: Why do Java and C# not support Multiple Inheritance with classes, while C++ does?

13. Summary

Inheritance allows you to build a hierarchy of classes, drastically reducing code duplication. By using the protected modifier, base classes can safely share data with derived classes. C++ is one of the few languages that supports both multilevel and multiple inheritance.

14. Next Chapter Recommendation

In Chapter 18: Polymorphism and Virtual Functions, we will explore how a Base class pointer can intelligently call the correct Derived class methods at runtime.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·