ambiguity problem and resolution in multiple inheritance, virtual base class, multipath inheritance

In multiple inheritance, there may be possibility that a class may inherit data member or member functions with same name from two or more base classes and the derived class may not have functions with same name as those of its base classes. Now If the derived class object needs to access one of the same named member function then ambiguity problem occurs as it is not clear to the compiler which base’s class member function should be invoked. The ambiguity means compiler is in confused state.
मल्टीप्ल इनहेरिटेंस में यह संभव है कि एक derived क्लास दो या दो से अधिक बेस क्लास से एक समान नाम के डाटा मेम्बर या मेम्बर फंक्शन को इन्हेरिट (विरासत) करे एवं derived क्लास में उस नाम का कोई फंक्शन परिभाषित ना हो।
अब यदि derived क्लास के ऑब्जेक्ट द्वारा उन दोनों फंक्शन में से किसी एक को कॉल किया जाता है तब एम्बीगुइटी समस्या उतपन्न होती है क्योंकि कम्पाइलर को यह स्पष्ट नहीं हो पता है कि कौन सी बेस क्लास का मेम्बर फंक्शन रन किया जावेगा। एम्बीगुइटी का अर्थ है कि कम्पाइलर उलझन की स्थिति में है।  

There are two solutions of ambiguity problem:-
एम्बीगुइटी समस्या के निम्न दो हल है :-

1.) The ambiguity can be resolved by using the scope resolution operator(::) to specify the class in which the member function lies. we can also use multilevel inheritance or virtual function or virtual base class to resolve ambiguity in multiple inheritance.
एम्बीगुइटी समस्या को स्कोप रिसलूशन ऑपरेटर (::) की सहयता से कॉल किये गए मेम्बर फंक्शन के साथ बेस क्लास का नाम दर्शाकर हल किया जा सकता है। हम मल्टीलेवल इनहेरिटेंस या वर्चुअल फंक्शन या वर्चुअल बेस क्लास का प्रयोग कर एम्बीगुइटी समस्या को हल कर सकते है।  

C++ program to resolve ambiguity problem of multiple inheritance
#include<iostream.h>
#include<conio.h>
class m{
protected:
int m;
public:
m(){
m=10;
}
void show(){
cout<<"M = "<<m<<endl;
}};
class n{
protected:
int n;
public:
n(){
n=20;
}
void show(){
cout<<"N = "<<n<<endl;
}};
class o:public m,public n{
protected:
int o;
public:
o(){
o=30;
}
void display(){
//show();         // ambiguity problem occurs
m::show();     //ambiguity resolution
n::show();     //ambiguity resolution
}};
void main(){
clrscr();
o obj;
obj.display();
getch();
}

No comments:

Post a Comment