Inheritance Ambiguity in C++
Last Updated :
21 Dec, 2022
Pre-requisites: Inheritance in C++, Multiple Inheritance in C++
In multiple inheritances, when one class is derived from two or more base classes then there may be a possibility that the base classes have functions with the same name, and the derived class may not have functions with that name as those of its base classes. If the derived class object needs to access one of the similarly named member functions of the base classes then it results in ambiguity because the compiler gets confused about which base’s class member function should be called.
Example:
C++
#include<iostream>
using namespace std;
class A {
public :
void func() {
cout << " I am in class A" << endl;
}
};
class B {
public :
void func() {
cout << " I am in class B" << endl;
}
};
class C: public A, public B {
};
int main() {
C obj;
obj.func();
return 0;
}
|
Output:
prog.cpp: In function ‘int main()’:
prog.cpp:43:9: error: request for member ‘func’ is ambiguous
obj.func();
^
prog.cpp:21:10: note: candidates are: void B::func()
void func() {
^
prog.cpp:11:10: note: void A::func()
void func() {
^
In this example, derived class C inherited the two base classes A and B having the same function name func(). When the object of class C is created and called the function func() then the compiler gets confused that which base class member function func() should be called.
Solution to Ambiguity:
To solve this ambiguity scope resolution operator is used denoted by ‘ :: ‘
Syntax:
ObjectName.ClassName::FunctionName();
Below is the program to show the concept of ambiguity resolution in multiple inheritances.
C++
#include<iostream>
using namespace std;
class A {
public :
void func() {
cout << " I am in class A" << endl;
}
};
class B {
public :
void func() {
cout << " I am in class B" << endl;
}
};
class C: public A, public B {
};
int main() {
C obj;
obj.A::func();
obj.B::func();
return 0;
}
|
Output
I am in class A
I am in class B
Code Snippet:
- We have created an “A” class which consists of the public member function “func”.
- We have created a “B” class which also consists of the public member function “func”.
- We have created a “C” class that is inheriting “A” and “B” classes.
- Object “obj” is created of the derived class “C”.
- The function “func” is called by the object “obj”.
The important thing to note here is that when the function “func” is called by the object “obj” first time it will invoke the function “func” of the “A” class and when the function “func” is called by the object “obj” second time it will invoke the function “func” of the “B” class because we had specified it using scope resolution operator “::” to get rid of ambiguity.
Function Overriding or Method Overriding
C++
#include<iostream>
using namespace std;
class A {
public :
void func() {
cout << " I am in class A" << endl;
}
};
class B {
public :
void func() {
cout << " I am in class B" << endl;
}
};
class C: public A, public B {
public :
void func() {
cout << " I am in class C" << endl;
}
};
int main() {
C obj;
obj.C::func();
obj.func();
return 0;
}
|
Output
I am in class C
I am in class C
In this example, we can see that we have declared function func() in derived class “C” also. So, when an object of class “C” is created and called the function func(), then, “I am in class C” is printed because it will override the base class “func” method because of function/method overriding. Now, if we do not specify which class member function “func” has to be called through the scope resolution operator then by default the compiler runs the method which is written in the body of the class through which instance of the object is created. But if the function “func” was not present in the derived class “C” then for calling function “func” in class “A” or “B”, we have to specify it through the scope resolution operator. So, the compiler will run the method of the specified class.
Another Solution (using virtual inheritance)
In C++, you can use virtual inheritance to resolve ambiguity in inheritance. Virtual inheritance is a way of specifying that a class should be inherited virtually, meaning that only one instance of the class should be present in the inheritance hierarchy, even if the class is inherited multiple times.
Below is the program to show the concept of ambiguity resolution in multiple inheritances.
C++
#include <iostream>
using namespace std;
class A {
public :
int x;
};
class B : virtual public A {
public :
int y;
};
class C : virtual public A {
public :
int z;
};
class D : public B, public C {
public :
int w;
};
int main() {
D obj;
obj.x = 1;
obj.y = 2;
obj.z = 3;
obj.w = 4;
return 0;
}
|
In this example, class A is inherited virtually by classes B and C, so when class D inherits from both B and C, there is no ambiguity in the inheritance of A. As a result, you can access the members of A directly through an instance of D, without any issues.
Note that virtual inheritance can have some performance overhead, as it requires the use of additional pointers to manage the inheritance hierarchy. Therefore, it should only be used when necessary to resolve ambiguity in inheritance.
Similar Reads
Inheritance in C++
The capability of a class to derive properties and characteristics from another class is called Inheritance. Inheritance is one of the most important features of Object-Oriented Programming in C++. In this article, we will learn about inheritance in C++, its modes and types along with the informatio
11 min read
Hybrid Inheritance In C++
Before jumping into Hybrid Inheritance, let us first know what is inheritance. Inheritance is a fundamental OOP concept in C++ that allows a new class, also known as a subclass or derived class, to inherit properties and methods from an already-existing class, also known as a superclass or base clas
6 min read
C++ Inheritance Access
Prerequisites: Class-Object in C++Inheritance in C++Before learning about Inheritance Access we need to know about access specifiers. There are three Access specifiers in C++. These are: public - members are accessible from outside the class, and members can be accessed from anywhere.private - membe
4 min read
Multiple Inheritance in C++
Multiple Inheritance is a feature of C++ where a class can inherit from more than one classes. The constructors of inherited classes are called in the same order in which they are inherited. For example, in the following program, B's constructor is called before A's constructor. A class can be deriv
5 min read
Inheritance and Friendship in C++
Inheritance in C++: This is an OOPS concept. It allows creating classes that are derived from other classes so that they automatically include some of the functionality of its base class and some functionality of its own. (See this article for reference) Friendship in C++: Usually, private and prote
2 min read
C++ Hierarchical Inheritance
Inheritance is a feature of Object-Oriented-programming in which a derived class (child class) inherits the property (data member and member functions) of the Base class (parent class). For example, a child inherits the traits of their parents. In Hierarchical inheritance, more than one sub-class in
4 min read
Comparison of Inheritance in C++ and Java
The purpose of inheritance is the same in C++ and Java. Inheritance is used in both languages for reusing code and/or creating an âis-aâ relationship. The following examples will demonstrate the differences between Java and C++ that provide support for inheritance. 1) In Java, all classes inherit fr
4 min read
Constructor in Multiple Inheritance in C++
Constructor is a class member function with the same name as the class. The main job of the constructor is to allocate memory for class objects. Constructor is automatically called when the object is created. Multiple Inheritance: Multiple Inheritance is a feature of C++ where a class can derive fro
2 min read
Interesting Facts about C++
C++ is a general-purpose, object-oriented programming language. It supports generic programming and low-level memory manipulation. Bjarne Stroustrup (Bell Labs) in 1979, introduced the C-With-Classes, and in 1983 with the C++. Here are some awesome facts about C++ that may interest you: The name of
2 min read
Constructor in Multilevel Inheritance in C++
Constructor is a class member function with the same name as the class. The main job of the constructor is to allocate memory for class objects. Constructor is automatically called when the object is created. Multilevel Inheritance Derivation of a class from another derived class is called Multileve
2 min read