What is Ambiguity Resolution in Inheritance?

Hi! viewers, today I am going to discuss ambiguity Resolution in Inheritance.

When a function with the same name appears in more than one base class, ambiguity might arise when implementing multiple inheritances.

Let’s understand this through an example:

Now Ilustrate The Code:

#include <iostream>  
using namespace std;  
class A  
{  
    public:  
    void display()  
    {  
        std::cout << "Class A" << std::endl;  
    }  
};  
class B  
{  
    public:  
    void display()  
    {  
        std::cout << "Class B" << std::endl;  
    }  
};  
class C : public A, public B  
{  
    void view()  
    {  
        display();  
    }  
};  
int main()  
{  
    C c;  
    c.display();  
    return 0;  
}  

Output:

error: reference to 'display' is ambiguous
        display();

The above problem can be handled by combining the function with the class resolution operator. The derived class code in the preceding example can be rewritten as:

class C : public A, public B  
{  
    void view()  
    {  
        A :: display();         // Calling the display() function of class A.  
        B :: display();         // Calling the display() function of class B.  
  
    }  
};  

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

In single inheritance, there is also the possibility of ambiguity.

Now consider the following problem:

class A  
{  
   public:  
void display()  
{  
   cout<<?Class A?;  
}   
} ;  
class B  
{   
  public:  
 void display()  
{  
 cout<<?Class B?;  
}  
} ;  

In single inheritance, there is also the possibility of ambiguity. In the example above, the derived class’s function overrides the base class’s method. As a result, calling the display() function just calls the derived class’s function. The class resolution operator can be used to call the base class function.

Join Our Community

Join our WhatsApp Group To know more about Programming Language tips, tricks and knowledge about and how to start learning any programming language.

Default image
Lingaraj Senapati
Hey There! I am Lingaraj Senapati, the Co-founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.
Articles: 217

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply