---
title: "Multipath inheritance problem and solution"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:multipath-inheritance-problem"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/multipath-inheritance-problem
site: BCT Engineering
---

# Multipath inheritance problem and solution

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2069 Chaitra, 2+4 marks; 2068 Chaitra, 3 marks (asked 2 times)
- Video: [MultiPath Inheritance In C++](https://www.youtube.com/watch?v=U0usxMpy_KE) — CppNuts
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/multipath-inheritance-problem
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

What kind of problem is encountered in multipath inheritance? Write down its solution with suitable example.

## Short answer

Multipath inheritance (diamond problem) causes a derived class to inherit multiple copies of a common base class, leading to ambiguity and inconsistency. The solution is to declare the common base as virtual in intermediate classes, ensuring only one shared copy.

## Answer

**Multipath inheritance** occurs when a derived class inherits from the same base class through more than one path. For example, Parent1 and Parent2 are both derived from Grandparent, and Child is derived from both Parent1 and Parent2. This diamond-shaped hierarchy is a form of hybrid inheritance.

```text
        Person
        /    \
   Student  Employee
        \    /
        Intern
```

#### Problem
`Intern` gets **two copies** of `Person`'s members: one through `Student` and one through `Employee`. Then:
- An access such as `obj.name` is **ambiguous**, and the compiler gives an error.
- Memory is wasted by the duplicate copies, and the data may become inconsistent.

#### Solution: virtual base class
Declare the common base as `virtual` in the intermediate classes. Then only one shared copy of `Person` is inherited.

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

class Person {
public:
    string name;
};
class Student : virtual public Person { };
class Employee : virtual public Person { };
class Intern : public Student, public Employee { };

int main() {
    Intern i;
    i.name = "Hari";            // no ambiguity: one copy only
    cout << i.name << endl;
    return 0;
}
```

**Output:** `Hari`

Without `virtual`, `i.name` would fail to compile; it would need `i.Student::name` or `i.Employee::name`, referring to two different copies.

## Check yourself

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

1. What problem arises in multipath (diamond) inheritance?
   A. The base class cannot be instantiated
   B. Two copies of the common base members, causing ambiguity
   C. Constructors never run
   D. Private members become public
   Answer: B. The final class inherits the common base twice, so members are duplicated and ambiguous. (Key point: Problem)

2. How is the problem solved?
   A. Make the final class virtual
   B. Use friend functions
   C. Declare the common base virtual in the intermediate classes
   D. Make all members private
   Answer: C. Writing virtual public Person in Student and Employee shares one copy. (Key point: Solution)

3. Without virtual, how could i.name be reached?
   A. It cannot be reached
   B. i.Person::Person
   C. i.name works fine
   D. i.Student::name or i.Employee::name
   Answer: D. Scope resolution picks one of the two copies. (Key point: Where virtual goes)

## Related in Object Oriented Programming

- [Effect of derivation types on class members](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aderivation-types-effect) — Object Oriented Programming, chapter 6

## Related in other subjects

- [TOH problem with solution for n disks](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Atoh-n-disks-solution) — Data Structure and Algorithm, chapter 5, asked 2×
- [Dining philosophers problem and solution](https://www.bctengineer.com/question.md?key=semester_6%2Foperating_system%3Adining-philosophers) — Operating System, chapter 3, asked 2×
- [Top-down integration problems; regression testing](https://www.bctengineer.com/question.md?key=semester_5%2Fsoftware_engineering%3Atop-down-integration-regression) — Software Engineering, chapter 8
- [Galerkin and collocation solution of an ODE](https://www.bctengineer.com/question.md?key=semester_8%2Ffinite_element_method%3Agalerkin-collocation-ode) — Finite Element Method, chapter 4
