---
title: "Need of virtual base class"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:virtual-base-class"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-base-class
site: BCT Engineering
---

# Need of virtual base class

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2072 Chaitra, 5 marks
- Video: [Virtual Base Class in C++](https://www.youtube.com/watch?v=fdSO5rsi_2k) — Neso Academy
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/virtual-base-class
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

Explain the need of virtual base class with suitable example.

## Short answer

In diamond inheritance, a base class is inherited multiple times causing ambiguity and duplication. Declaring the intermediate classes with virtual base ensures only one shared sub-object; the most derived class constructs it, resolving ambiguity.

## Answer

A **virtual base class** is a base class declared with the keyword `virtual` in the derived classes' inheritance list, so that when it is inherited through multiple paths, only **one shared copy** of it is present in the final derived class.

#### Need
In multipath (diamond) inheritance, the final class inherits the common base twice - once through each intermediate class. This creates:
1. **Ambiguity** - `obj.data` could mean either copy, and the compiler reports an error.
2. **Duplication** - two copies waste memory and can hold different values.

```text
          Person              (inherited twice without virtual)
          /    \
     Student  Employee
          \    /
          Intern
```

#### Example - without virtual (error)

```cpp
class Person { public: int id; };
class Student : public Person {};
class Employee : public Person {};
class Intern : public Student, public Employee {};
// Intern x;  x.id = 5;   // error: request for 'id' is ambiguous
```

#### Example - with virtual (works)

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

class Person {
public:
    int id;
};
class Student : virtual public Person {};     // virtual
class Employee : virtual public Person {};    // virtual
class Intern : public Student, public Employee {};

int main() {
    Intern x;
    x.id = 5;                    // one copy of id, no ambiguity
    cout << "id = " << x.id << endl;
    return 0;
}
```

**Output:** `id = 5`

`virtual` must be written in the intermediate classes `Student` and `Employee`, not in `Intern`. The most derived class constructs the shared virtual base first.

## Check yourself

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

1. What does a virtual base class ensure?
   A. Two copies per path
   B. That the base cannot have data
   C. Only one shared copy of the base in the final derived class
   D. That the base is abstract
   Answer: C. It makes the common base shared across paths. (Key point: Definition)

2. Where must the keyword virtual be written?
   A. In the final class Intern
   B. In the base class Person
   C. In main
   D. In the intermediate classes Student and Employee
   Answer: D. Virtual is written in the inheritance lists of the intermediate classes. (Key point: Placement)

3. Without virtual, why does x.id = 5 fail in Intern?
   A. id is private
   B. id is ambiguous because there are two copies
   C. id is static
   D. Intern has no constructor
   Answer: B. Intern contains two Person sub-objects, so id is ambiguous. (Key point: Need)

4. Which class constructs the shared virtual base?
   A. Student
   B. Employee
   C. Person's child alphabetically
   D. The most derived class
   Answer: D. The most derived class constructs the virtual base first. (Key point: Construction)

## Related in Object Oriented Programming

- [Pure virtual function and abstract class](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apure-virtual-abstract-class) — Object Oriented Programming, chapter 7, asked 5×
- [Need of virtual function](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-function-need) — Object Oriented Programming, chapter 7, asked 4×
- [Virtual destructor with example](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avirtual-destructor) — Object Oriented Programming, chapter 7
- [Abstract Student class program](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Astudent-abstract-program) — Object Oriented Programming, chapter 7
- [Polygon abstract class with Area()](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apolygon-abstract-area) — Object Oriented Programming, chapter 7
- [Constructor/destructor order; parameterized base constructor](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Actor-dtor-order-single-multiple) — Object Oriented Programming, chapter 6, asked 2×

## Related in other subjects

- [Need of classless IP addressing](https://www.bctengineer.com/question.md?key=semester_7%2Fcomputer_networks_and_security%3Aclassless-need-example) — Computer Networks and Security, chapter 4
