Lesson 23 +35 XP

Types of Inheritance & Class Hierarchies

Types of Inheritance & Class Hierarchies

Classes can be connected by inheritance in several patterns, from a single parent and child to whole family trees. C++ supports all five classic forms: single, multilevel, multiple, hierarchical and hybrid inheritance. This lesson gives each form with a diagram and a complete program, and explains how classes form a class hierarchy.

The five types of inheritanceThe five types of inheritance

TypeShapeExample
Singleone base → one derivedEmployee → Manager
Multilevela chain: A → B → CStudent → Test → Result
Multipleseveral bases → one derivedInternal, External → Result
Hierarchicalone base → several derivedShape → Circle, Rectangle, Triangle
Hybrida combination of two or more of the abovemultilevel + multiple

1. Single Inheritance

One derived class inherits from one base class. (A full program is in Lesson 22.)

C++
#include <iostream>
using namespace std;

class Vehicle {
protected:
    int wheels;

public:
    Vehicle() {
        wheels = 0;
    }
    void showWheels() {
        cout << "Wheels: " << wheels << endl;
    }
};

class Car : public Vehicle {
public:
    Car() {
        wheels = 4;
    }
    void honk() {
        cout << "Car says beep" << endl;
    }
};

int main() {
    Car c;
    c.showWheels();
    c.honk();
    return 0;
}

Output:

OUTPUT
Wheels: 4
Car says beep

2. Multilevel Inheritance

A class is derived from a class that is itself derived: A → B → C. Class B is an intermediate base class: it is a derived class of A and a base class of C. The chain A → B → C is called the inheritance path.

The classic textbook program: Student holds the roll number, Test adds the marks, Result adds the total.

C++
#include <iostream>
using namespace std;

class Student {
protected:
    int rollNumber;

public:
    void getNumber(int a) {
        rollNumber = a;
    }
    void putNumber() {
        cout << "Roll Number: " << rollNumber << endl;
    }
};

class Test : public Student { // first level of derivation
protected:
    float sub1, sub2;

public:
    void getMarks(float x, float y) {
        sub1 = x;
        sub2 = y;
    }
    void putMarks() {
        cout << "Marks in Subject 1 = " << sub1 << endl;
        cout << "Marks in Subject 2 = " << sub2 << endl;
    }
};

class Result : public Test { // second level of derivation
    float total;

public:
    void display() {
        total = sub1 + sub2; // uses members inherited through Test
        putNumber();         // from Student, two levels up
        putMarks();
        cout << "Total = " << total << endl;
    }
};

int main() {
    Result student1;
    student1.getNumber(111);
    student1.getMarks(75.0, 59.5);
    student1.display();
    return 0;
}

Output:

OUTPUT
Roll Number: 111
Marks in Subject 1 = 75
Marks in Subject 2 = 59.5
Total = 134.5

3. Multiple Inheritance

A class is derived from two or more base classes, combining their features. The base classes are listed, separated by commas, each with its own visibility mode:

C++
class D : public B1, public B2 {
    ...
};

In the example below, Result combines internal and external marks inherited from two base classes:

C++
#include <iostream>
using namespace std;

class Internal {
protected:
    int internalMarks;

public:
    void getInternal(int m) {
        internalMarks = m;
    }
};

class External {
protected:
    int externalMarks;

public:
    void getExternal(int m) {
        externalMarks = m;
    }
};

class Result : public Internal, public External { // two base classes
public:
    void display() {
        cout << "Internal: " << internalMarks << endl;
        cout << "External: " << externalMarks << endl;
        cout << "Total:    " << internalMarks + externalMarks << " / 100" << endl;
    }
};

int main() {
    Result r;
    r.getInternal(22); // from Internal
    r.getExternal(61); // from External
    r.display();
    return 0;
}

Output:

OUTPUT
Internal: 22
External: 61
Total:    83 / 100

Ambiguity in multiple inheritance

If two base classes have a member function with the same name, a call through the derived object is ambiguous. It is resolved with the scope resolution operator, or by redefining the function in the derived class.

C++
#include <iostream>
using namespace std;

class M {
public:
    void display() {
        cout << "Class M" << endl;
    }
};

class N {
public:
    void display() {
        cout << "Class N" << endl;
    }
};

class P : public M, public N {
public:
    void display() {  // redefinition resolves the clash for P's users
        M::display(); // choose explicitly
        N::display();
    }
};

int main() {
    P p;
    p.display();    // P's own display
    p.M::display(); // or pick one from outside
    return 0;
}

Output:

OUTPUT
Class M
Class N
Class M

Multiple inheritance can also inherit the same grandparent twice (the diamond problem). That is solved with virtual base classes, covered in Lesson 30.

4. Hierarchical Inheritance

Several classes are derived from one base class. The common features go in the base class; each derived class adds its own. This mirrors hierarchical classification in the real world.

C++
#include <iostream>
using namespace std;

class Shape {
protected:
    double a, b;

public:
    void setDims(double x, double y = 0) {
        a = x;
        b = y;
    }
};

class Rectangle : public Shape {
public:
    double area() {
        return a * b;
    }
};

class Triangle : public Shape {
public:
    double area() {
        return 0.5 * a * b;
    }
};

class Circle : public Shape {
public:
    double area() {
        return 3.14159 * a * a;
    }
};

int main() {
    Rectangle r;
    r.setDims(4, 5);
    Triangle t;
    t.setDims(4, 5);
    Circle c;
    c.setDims(2);
    cout << "Rectangle area: " << r.area() << endl;
    cout << "Triangle area:  " << t.area() << endl;
    cout << "Circle area:    " << c.area() << endl;
    return 0;
}

Output:

OUTPUT
Rectangle area: 20
Triangle area:  10
Circle area:    12.5664

5. Hybrid Inheritance

Hybrid inheritance combines two or more types. In the classic example, Result is derived from Test (which is derived from Student: multilevel) and also from Sports (multiple).

C++
#include <iostream>
using namespace std;

class Student {
protected:
    int roll;

public:
    void getRoll(int r) {
        roll = r;
    }
    void putRoll() {
        cout << "Roll No: " << roll << endl;
    }
};

class Test : public Student { // multilevel part
protected:
    float part1, part2;

public:
    void getMarks(float x, float y) {
        part1 = x;
        part2 = y;
    }
    void putMarks() {
        cout << "Marks: " << part1 << " + " << part2 << endl;
    }
};

class Sports {
protected:
    float score;

public:
    void getScore(float s) {
        score = s;
    }
    void putScore() {
        cout << "Sports weightage: " << score << endl;
    }
};

class Result : public Test, public Sports { // multiple part
    float total;

public:
    void display() {
        total = part1 + part2 + score;
        putRoll();
        putMarks();
        putScore();
        cout << "Total score: " << total << endl;
    }
};

int main() {
    Result s;
    s.getRoll(1234);
    s.getMarks(27.5, 33.0);
    s.getScore(6.0);
    s.display();
    return 0;
}

Output:

OUTPUT
Roll No: 1234
Marks: 27.5 + 33
Sports weightage: 6
Total score: 66.5

6. Class Hierarchies

When many classes are related by inheritance they form a class hierarchy: a tree (or graph, with multiple inheritance) with the most general class at the top and the most specialised classes at the bottom. Moving down the hierarchy is specialisation; moving up is generalisation.

A class hierarchy: generalisation upwards, specialisation downwardsA class hierarchy: generalisation upwards, specialisation downwards

Guidelines for designing a hierarchy:

  1. Put common data and behaviour as high as possible, so they are written once.
  2. Each derived class should pass the "is-a" test with its base class.
  3. Classes near the top are often abstract (they describe a concept, not a concrete thing; Lesson 29).
  4. Keep hierarchies shallow: very deep chains are hard to understand.
💡 Tip

Key points to remember

  • Single: A → B. Multilevel: A → B → C (B is an intermediate base). Multiple: A, B → C. Hierarchical: A → B, C, D. Hybrid: a mix.
  • Multiple inheritance syntax: class C : public A, public B.
  • A name clash in multiple inheritance is resolved with A::f() or by redefining f in the derived class.
  • The diamond (same grandparent twice) needs a virtual base class (Lesson 30).
  • A class hierarchy goes from general (top) to specialised (bottom).
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Explain the different types of inheritance with diagrams and examples. (10 Marks)
  • Write: the diagram, the table, and a short code skeleton for each of the five types.
  • Q2. Write a program to implement multilevel inheritance using Student, Test and Result classes. (5 Marks)
  • Q3. What is ambiguity in multiple inheritance? How is it resolved? (5 Marks)
  • Write: the M/N/P program; scope resolution and redefinition.
  • Q4. Write a program to implement hybrid inheritance. (5 Marks)
  • Q5. What is a class hierarchy? Explain generalisation and specialisation. (2.5 Marks)