Topic 6

Inheritance & Inheritance Hierarchies

2 min read3 questions to test yourself

1Definition

Inheritance is the mechanism by which one class (the derived/child class) acquires the attributes and behaviors of another class (the base/parent class). It models an IS-A relationship and promotes code reusability.

2Simple Explanation

Just as a Child inherits physical traits (like eye color or height) from their Parents, a derived class automatically inherits variables and methods defined in its base class. The child class can reuse inherited features as-is, modify (override) them, or add brand-new features.

3Real-Life Example / Analogy

  • Mother & Daughter: A daughter inherits facial features and voice tone from her mother, but she can also acquire new skills like playing the violin.
  • Vehicle Taxonomy: A Vehicle parent class defines speed and fuel. Car and Bike inherit these properties, adding specific features like airbags or kickStart.

4Diagram / Visual Representation

Single

Multilevel

Hierarchical

Hierarchical

Multiple / Hybrid

A: Base class

B: Derived class

C: Derived class

D: Further derived class

Shared virtual base A

B

C

D: one shared A subobject

5Working / How It Works

  • C++ Syntax: class DerivedClass : visibility-mode BaseClass
  • Visibility Modes:
Base Member Access public Inheritance protected Inheritance private Inheritance
private Inaccessible Inaccessible Inaccessible
protected protected protected private
public public protected private

6Types / Components / Characteristics

  1. Single Inheritance: 1 Base Class →\rightarrow 1 Derived Class.
  2. Multilevel Inheritance: Class A →\rightarrow Class B →\rightarrow Class C.
  3. Hierarchical Inheritance: 1 Base Class →\rightarrow Multiple Derived Classes.
  4. Multiple Inheritance: Multiple Base Classes →\rightarrow 1 Derived Class.
  5. Hybrid Inheritance: Combination of two or more inheritance types.
  6. Virtual Inheritance: Resolves the Diamond Problem (duplicate base sub-objects in multiple inheritance paths).

7Solved Examples / Problems

Problem Statement: Write a C++ program demonstrating Multiple Inheritance and Virtual Base Classes to solve the Diamond Problem.

C++
#include <iostream>
using namespace std;
class Animal {
public:
    int age;
    Animal() { age = 10; }
};
// Virtual inheritance avoids duplicate Animal instances
class Mammal : virtual public Animal {};
class WingedAnimal : virtual public Animal {};
class Bat : public Mammal, public WingedAnimal {};
int main() {
    Bat b;
    cout << "Bat Age: " << b.age << endl; // Unambiguous access
    return 0;
}

Output:

Output
Bat Age: 10

8Advantages, Disadvantages & Applications

  • Advantages:
    • Reusability of established base class functionality without rewriting code.
    • Establishes clear domain hierarchies.
  • Disadvantages:
    • Tight coupling between parent and child classes.
    • Misuse can lead to deep, unmaintainable class hierarchies.
  • Applications: GUI Widget frameworks (e.g., Window →\rightarrow Dialog →\rightarrow FileDialog).

9Important Points to Remember

  • Constructors are executed in order from Base to Derived; Destructors run in reverse order (Derived to Base).
  • Virtual inheritance ensures only one shared copy of a common base class is created in memory.

10Test Your Understanding

  1. Conceptual Question: What is the Diamond Problem in Multiple Inheritance?
    Show answer
    When a class inherits from two derived classes that share a common base class, leading to duplicate base sub-objects and compiler ambiguity.
  2. MCQ: Under private visibility mode, public members of the base class become what in the derived class?
    • A) Public
    • B) Protected
    • C) Private
    • D) Inaccessible
    Show answer
    C) Private
  3. Application Question: Trace constructor call order for class C : public B, public A.
    Show answer
    B's constructor runs first, followed by A's constructor, and finally C's constructor.

Finished this topic?

0 of 14 topics read in Object-Oriented Programming

Handwritten notes for the exam

Unit-wise SPPU notes and PYQs for this subject.

Helpful? Share it with your class

Free for everyone, no login needed.

Share topic