Ambiguity is a problem that surfaces in certain situations involving multiple inheritance. Consider the following cases:
? Base classes having functions with the same name.
? The class derived from base basses is not having a function with the name as those of
its base classes.
? Members of a derived class or its objects referring to a member, whose name is the
same as those in base classes.
These situations create ambiguity in deciding which of the base class’s function has to be referred. The problem is resolved using the scope resolution operator. The syntax for handling ambiguity in multiple inheritance.
ObjectName.BaseClassName::MemberName(…)
Sample Program
#include <iostream.h>
class A
{
char ch;
public:
A(char c)
{
ch = c;
}
void show(void)
{
cout<<ch;
}
};
class B
{
char ch;
public:
B(char b)
{
ch = b;
}
void show(void)
{
cout<<ch;
}
};
class C:public A, public B
{
char ch;
public:
C(char c1, char c2, char c3):A(c1), B(c2)
{
ch = c3;
}
};
void main()
{
C obj(‘a’,’b’,’c’);
Cout<<”A class show”;
Obj.A::show();
Cout<<”B class show”;
Obj.B::show();
// obj.show //Error, Because it creates the ambiguity
}
No comments:
Post a Comment