Ambiguity in Multiple Inheritances





Ambiguity in multiple inheritances

Consider an example of multiple inheritances in which there are two base classes having functions with the same name and a class derived from both these base classes having no function with this name.

 

#include<iostream.h>

#include<conio.h>

Class A

{

Public:

Void show ()

{

Cout<<”\n class A”;

}

};

Class B

{

Public:

Void show ()

{

Cout<<”\n class B”;

}

};

Class C: public A, public B

{

};

Void main (0

{

C obj;

Obj. Show ();

}

The members are ambiguous without scope resolution operator when the member function show () is accessed by the derived class object, naturally, the compiler can’t distinguish between member function of the class A and B.

The problem is resolved using scope-resolution operator to specify the class in which the function lies. Thus,

Obj.y:: show ();

Refers to the function in the B class. Thus,  the scope – resolution operator resolves the ambiguity.



Related posts:

  1. Overriding Member Function in C++ Overriding member function We can define data member and member...
  2. Virtual base class in C++ Virtual base class in C++ Consider the situation, with a...
  3. Inheritance in C++ Inheritance in C++ Inheritance is one of the most important...
  4. Defining Member Function In C++ Class methods definitions  (Defining the member functions) Member functions can...
  5. Type of Inheritance Type of Inheritance Multiple inheritance When a derived class is...