---
title: "Ambiguity and how to resolve it"
subject: "Object Oriented Programming"
chapter: 6
key: "semester_3/object_oriented_programming:ambiguity-resolve-example"
source: https://www.bctengineer.com/q/semester_3/object_oriented_programming/ambiguity-resolve-example
site: BCT Engineering
---

# Ambiguity and how to resolve it

Object Oriented Programming · Chapter 6: Inheritance

- Asked in: 2074 Chaitra, 4 marks
- Video: [Ambiguity Resolution in Inheritance in C++ | C++ Tutorials for Beginners #43](https://www.youtube.com/watch?v=ZqfArYoV9Lg) — CodeWithHarry
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_3/object_oriented_programming/ambiguity-resolve-example
- Whole chapter: https://www.bctengineer.com/md/semester_3/object_oriented_programming/chapter-6

## Question

What is ambiguity? How can it be resolved? Explain with a suitable example.

## Short answer

Ambiguity arises when a derived class has the same member name from multiple base classes or multiple copies. Resolution methods: use the scope resolution operator (Base::member), override the member in the derived class, or use virtual base class for diamond problem to have a single copy.

## Answer

**Ambiguity** is a situation in which the compiler cannot decide which member to use because the same name is available from more than one place. In inheritance it occurs when:
1. Two base classes (multiple inheritance) have a member with the same name, and the derived class uses it.
2. A class inherits the same base through two paths (diamond problem), giving two copies of its members.

**Example:**

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

class A {
public:
    void show() { cout << "Class A" << endl; }
};
class B {
public:
    void show() { cout << "Class B" << endl; }
};
class C : public A, public B { };

int main() {
    C obj;
    // obj.show();      // ERROR: ambiguous - A::show() or B::show()?
    obj.A::show();      // resolved with scope resolution operator
    obj.B::show();
    return 0;
}
```

**Output:**

```text
Class A
Class B
```

#### Ways to resolve ambiguity
1. **Scope resolution operator** `::` - name the class explicitly, as in `obj.A::show()`.
2. **Override in the derived class** - define `show()` in `C`, which then hides both and can call `A::show()` and `B::show()` as needed.
3. **Virtual base class** - for the diamond problem, declare the common base as `virtual` so only one copy exists.

## Check yourself

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

1. What is ambiguity in inheritance?
   A. The compiler cannot decide which member to use because the name comes from more than one place
   B. A syntax error in a class
   C. A missing return type
   D. A private member being accessed
   Answer: A. Same-named members from several bases (or duplicated bases) confuse the compiler. (Key point: Definition)

2. If A and B both have show() and C inherits both, how do you call A's version?
   A. obj.show()
   B. obj->A()
   C. obj.A::show()
   D. show(A)
   Answer: C. The scope resolution operator names the class explicitly. (Key point: Resolve by scope)

3. Which technique resolves ambiguity from the diamond problem?
   A. Friend function
   B. Inline function
   C. Static member
   D. Virtual base class
   Answer: D. A virtual base leaves only one copy of the common base. (Key point: Diamond)

4. Another way to resolve the show() ambiguity is to...
   A. define show() in the derived class C
   B. make C abstract only
   C. delete A
   D. rename main
   Answer: A. A show() in C hides both and can call A::show() and B::show(). (Key point: Another way)

## Related in Object Oriented Programming

- [Function overriding and how to resolve it](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afunction-overriding-resolve) — Object Oriented Programming, chapter 6
