Topic 49 of 91 8. Object-Oriented Programming in C++ Interactive Tutorial

Inheritance in C++: Reusing and Extending Class Functionality

8 mins read Last updated: 26 Sep 2026 14,713 views

Direct Answer & Core Concept

Inheritance allows a derived class to inherit member variables and functions from one or more base classes. C++ supports Single, Multilevel, Multiple, Hierarchical, and Hybrid inheritance. The Diamond Problem (duplicate base class subobjects in multiple inheritance) is resolved using Virtual Inheritance (virtual public Base).

Detailed Explanation & Mechanics

1. Inheritance Forms:

  • Single: class B : public A
  • Multilevel: class C : public B
  • Multiple: class C : public A, public B

2. The Diamond Problem & Virtual Inheritance:

Architecture Diagram
┌───────────┐
      │  Device   │
      └─────┬─────┘
       ┌────┴────┐
 ┌─────┴───┐ ┌───┴─────┐
 │ Printer │ │ Scanner │
 └─────┬───┘ └───┬─────┘
       └────┬────┘
      ┌─────┴─────┐
      │  Copier   │ (Without virtual inheritance: 2 copies of Device!)
      └───────────┘
With class Printer : virtual public Device and class Scanner : virtual public Device, Copier receives only ONE shared instance of Device.

Runnable Code Example: Inheritance in C++

C++20 / C++23
main.cpp
Run Online
#include <iostream>
#include <string>

// Virtual Base Class
class Device {
public:
    int deviceId;
    explicit Device(int id) : deviceId(id) {
        std::cout << "Device(" << deviceId << ") initialized.\n";
    }
};

class Printer : virtual public Device {
public:
    Printer(int id) : Device(id) {}
    void printDocument() { std::cout << "Printing document on device " << deviceId << "\n"; }
};

class Scanner : virtual public Device {
public:
    Scanner(int id) : Device(id) {}
    void scanDocument() { std::cout << "Scanning document on device " << deviceId << "\n"; }
};

// Most derived class initializes the virtual base
class MultiFunctionPrinter : public Printer, public Scanner {
public:
    MultiFunctionPrinter(int id) : Device(id), Printer(id), Scanner(id) {}
};

int main() {
    MultiFunctionPrinter mfp(7001);
    mfp.printDocument();
    mfp.scanDocument();
    std::cout << "Shared Device ID: " << mfp.deviceId << "\n"; // Unambiguous!
    return 0;
}

Common C++ Technical Interview Questions & Answers

Interview Prep
Q

Q: What is the order of constructor calls in multiple inheritance?

A: Base class constructors are called in the order they are listed in the class derivation declaration, followed by member constructors, followed by the derived constructor body.
Q

Q: Why does the most derived class initialize virtual base classes directly?

A: In virtual inheritance, the shared virtual base is instantiated only once at the top of the hierarchy; thus the most derived class is responsible for passing arguments to it.

Key Takeaways, Modern Best Practices & Idioms

C++ Best Practices & Production Rules

• Prefer composition ("has-a") over multiple inheritance where possible.
• Use virtual inheritance when designing diamond-shaped hierarchies to eliminate data duplication and ambiguity.
• Always pass arguments to base constructors via member initialization lists.
We value your privacy & transparency

OperateTools uses cookies and local storage to ensure basic tool functionality, preserve preferences, and analyze aggregated traffic. We never sell your personal data or index your document files. Learn more in our Privacy Policy.