Describe about ambiguity and its resolution in case of multiple inheritance in C++?

In multiple inheritance, there are multiple base classes and a single derived class.

 

If more than 1 base classes have same members name, which are being inherited in a derived class…

And if we create derived class object and access those members, then the compiler gets confused {which version of the member and of which base class is called}.

 

 This is called ambiguity in multiple inheritance.

May be it is not still clear to you. But believe me, after you complete this blog post, this will be crystal clear. Be patient to invest 4-5 minutes in this blog posts with your pen and paper for writing answers through this blog posts.  

In order to resolve this, while calling those members with derived class objects, base class name and scope resolution operator is used.

Source code-:

 

Output-:

enter the value to x of a base class3
enter the value to x of a base 2 class6
enter the value to z of a derived class5
the sum is14

 

Clue for understanding program-:

This program is confusing after you have done the accessing overriden members using 2 methods in the previous blog posts.

here 2 base classes had same name (name=member and member fxn name). but in previous cases we were making single base class and either single or multiple derived classes.

now the problem here is that, since 2 base classes have the same name…as soon as we create a derived class, the compiler will get confused about which information of the basee class to use (since we have base classes base1 AND base2….it is same as calling someone as Vishal and there are 2 Vishal in your class, now both Vishals will get confused. Here also both the derived classes gets confused…. ) So there must be something done for clear identification to the derived class.

In the past case, bringing the member function name like base::fxn_name() inside the derived class function name worked pretty much well.

But it is very clear that the same method can’t be used here since we already have 2 base classes with the same name.

So, we need the case of ambiguity and its resolution in multiple inheritance.

Hence it was resolved using scope resoltion operator as

Base1::x+Base2::x+z.

but what about the function calls inside main function?

it was also done in somehow similar way.

[ we could not use D.get_x() because get_x() is not in derived class and  since 2 derived classes have the same fxn name, it won’t make any sense and generate error]

in the main function also we have taken the help of scope resolution operator as D.Base1::get_x();

D.Base2::get_x(); [ read this as the get_x() function of the base class base2 called by derived class object D]

D.getz();

So that’s it.

This one was a tough concept to understand but I am sure everyone can write this properly if asked in exams.

Make sure you have written this in your copy, typed the source code yourself and tested the program in your PC.

Leave a Comment

Your email address will not be published. Required fields are marked *

Do NOT follow this link or you will be banned from the site! Scroll to Top