---
title: "Hybrid inheritance with example"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:hybrid-inheritance"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/hybrid-inheritance
site: BCT Engineering
---

# Hybrid inheritance with example

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2072 Kartik, 6 marks
- Video: [Hybrid Inheritance in C++ with Diamond Problem and its Resolution | Virtual Inheritance in C++](https://www.youtube.com/watch?v=DiUXoiOLZY0) — Simple Snippets
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/hybrid-inheritance
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

With suitable example explain hybrid inheritance.

## Short answer

Hybrid inheritance combines multiple inheritance types, like diamond: hierarchical then multiple. Example: class Result inherits Test and Sports, both from Student; using virtual base class avoids duplicate Student sub-objects.

## Answer

**Hybrid inheritance** is a combination of two or more types of inheritance (single, multilevel, multiple, hierarchical) in one program. A common form is a diamond: hierarchical inheritance followed by multiple inheritance.

```text
          Student
          /     \
       Test    Sports
          \     /
          Result
```

`Test` and `Sports` are derived from `Student` (hierarchical); `Result` is derived from both `Test` and `Sports` (multiple). Since `Result` would get two copies of `Student`, `Student` is made a **virtual base class** to avoid ambiguity.

```cpp
#include <iostream>
using namespace std;

class Student {
protected:
    int roll;
public:
    void getRoll(int r) { roll = r; }
};

class Test : virtual public Student {
protected:
    float m1, m2;
public:
    void getMarks(float a, float b) { m1 = a; m2 = b; }
};

class Sports : virtual public Student {
protected:
    float score;
public:
    void getScore(float s) { score = s; }
};

class Result : public Test, public Sports {
public:
    void display() {
        cout << "Roll no : " << roll << endl;
        cout << "Marks   : " << m1 << ", " << m2 << endl;
        cout << "Sports  : " << score << endl;
        cout << "Total   : " << m1 + m2 + score << endl;
    }
};

int main() {
    Result r;
    r.getRoll(101);
    r.getMarks(70, 80);
    r.getScore(10);
    r.display();
    return 0;
}
```

**Output:**

```text
Roll no : 101
Marks   : 70, 80
Sports  : 10
Total   : 160
```

Here `roll` is accessed in `Result` without ambiguity because only one `Student` sub-object exists.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What is hybrid inheritance?
   A. Inheritance with only one base
   B. A combination of two or more inheritance types
   C. Inheritance without classes
   D. Inheritance using templates
   Answer: B. It combines types such as hierarchical and multiple in one program. (Key point: Definition)

2. In Student -> Test, Sports -> Result, which types are combined?
   A. Hierarchical then multiple
   B. Single and multilevel only
   C. Only multiple
   D. Only single
   Answer: A. Test and Sports derive from Student (hierarchical); Result derives from both (multiple). (Key point: Diamond example)

3. Why is Student a virtual base class?
   A. To make it abstract
   B. To speed up calls
   C. To make roll private
   D. To give Result only one Student sub-object
   Answer: D. Without virtual, Result would get two Student copies and roll would be ambiguous. (Key point: Why virtual)

4. With marks 70, 80 and sports score 10, what total is printed?
   A. 160
   B. 150
   C. 80
   D. 101
   Answer: A. 70 + 80 + 10 = 160. (Key point: Total)
