Inheritance in C++

Unit 3 Β· Topic 3.1 ⏱ 12 min read πŸ“Š 2 diagrams ❓ 5 MCQs

Introduction

Inheritance is one of the four pillars of Object-Oriented Programming (OOP). It allows a new class (called the derived or child class) to acquire the properties and behaviors of an existing class (called the base or parent class). This promotes code reusability, extensibility, and models real-world "is-a" relationships.

For example, a Car is a Vehicle, and a Bike is also a Vehicle. Instead of rewriting speed, fuel, start() and stop() for each, we put them once in Vehicle and let Car and Bike inherit them.

UML Class Diagram

C++ inheritance UML diagram showing base class Vehicle with derived classes Car and Bike

Fig 3.1 β€” Vehicle is the base class; Car and Bike publicly inherit from it (is-a relationship).

Definition

Inheritance: A mechanism by which a derived class acquires all the (non-private) members of a base class, enabling code reuse and hierarchical classification of types.

Syntax in C++:

class DerivedClass : access-specifier BaseClass {
    // additional members of DerivedClass
};

The access-specifier can be public, private, or protected. It controls how the inherited members are accessed in the derived class.

Types of Inheritance (Access Specifiers)

Public Inheritance

Base public β†’ Derived public; base protected β†’ Derived protected.

Represents a true "is-a" relationship. Most common.

Private Inheritance

All inherited members become private in derived class. Used for "implemented-in-terms-of".

Protected Inheritance

Inherited members become protected. Rare; used in framework design.

Multiple Inheritance

A derived class inherits from more than one base class. Powerful but needs care (diamond problem).

Inheritance Types Tree

classDiagram Inheritance <|-- Single Inheritance <|-- Multiple Inheritance <|-- Multilevel Inheritance <|-- Hierarchical Inheritance <|-- Hybrid

Fig 3.2 β€” Classification of inheritance by number of base/derived classes.

C++ Code Example

A complete example demonstrating public inheritance:

#include <iostream>
using namespace std;

class Vehicle {
protected:
    int speed;
public:
    Vehicle() : speed(0) {}
    void start() { cout << "Vehicle started\n"; }
    void stop()  { cout << "Vehicle stopped\n"; }
};

class Car : public Vehicle {
private:
    int doors;
public:
    Car(int d) : doors(d) {}
    void honk() { cout << "Car honks! Doors: " << doors << "\n"; }
};

class Bike : public Vehicle {
private:
    int gears;
public:
    Bike(int g) : gears(g) {}
    void wheelie() { cout << "Bike does a wheelie! Gears: " << gears << "\n"; }
};

int main() {
    Car c(4);
    c.start();
    c.honk();
    c.stop();

    Bike b(6);
    b.start();
    b.wheelie();
    b.stop();
    return 0;
}

Real-World Example

Think of an Employee base class (name, id, salary) and derived classes Manager, Developer, Designer. Each derived class reuses common data and adds its own (e.g., Developer has programmingLanguage).

Advantages & Disadvantages

βœ… Advantages

  • Code reusability
  • Extensibility (add features easily)
  • Polymorphism support
  • Logical classification (is-a)

❌ Disadvantages

  • Tight coupling between base & derived
  • Multiple inheritance complexity
  • Can break encapsulation if misused
  • Deep hierarchies are hard to maintain

Important Questions

Short Questions

  1. Define inheritance in OOP.
  2. Differentiate between base class and derived class.
  3. What is the default access specifier in C++ class inheritance?
  4. Name the types of inheritance in C++.

Long Questions

  1. Explain public, private and protected inheritance with examples.
  2. Discuss advantages and disadvantages of inheritance.
  3. Write a C++ program demonstrating multilevel inheritance.

MCQ Practice

1. Which keyword is used to inherit a class in C++?

  • inherits
  • :
  • extends
  • implements
Answer: : β€” e.g. class Car : public Vehicle.

2. In public inheritance, a public member of base becomes:

  • private in derived
  • public in derived
  • protected in derived
  • inaccessible
Answer: public in derived.

3. Which inheritance type represents a true "is-a" relationship?

  • Public inheritance
  • Private inheritance
  • Protected inheritance
  • Multiple inheritance
Answer: Public inheritance.

4. The problem where a class inherits the same base twice through multiple paths is called:

  • Diamond of death
  • Diamond problem
  • Ambiguity error
  • Virtual clash
Answer: Diamond problem β€” solved using virtual inheritance.

5. Which of the following is NOT a type of inheritance?

  • Single
  • Multiple
  • Multilevel
  • Circular
Answer: Circular is not a standard inheritance type.

Quick Revision

  • Inheritance = derived class acquires base class members.
  • Access specifiers: public, private, protected.
  • Types: single, multiple, multilevel, hierarchical, hybrid.
  • Public inheritance β†’ true "is-a" relationship.
  • Solves code reuse; beware of tight coupling.