Multiple Inheritance Problem In C++
Ambiguity in Multiple Inheritance
Suppose we have 3 classes first class is A and second class is B and the last class is C which is derived from A and B.In A class we have one method void display() same for class B.
Code
class A
{
public :
void display(){count<<"This Is Class A "<<endl;}
};
class B
{
public :
void display(){count<<"This Is Class B "<<endl;}
};
class C :public A, public B
{
};
Main Method
In main method make an object of class C and call the method display().This is the actually problem in multiple inheritance in c++ because the system will not know which display() method should execute.The execution of class A or class B.
Code
int main()
{
C obj;
obj.display() // here is the problem
}
Solution
This problem is solved by scoperesolution operator with class name.
Code Solution
int main()
{
C obj;
// obj.display() // here is the problem
obj.A::display();
obj.B::display();
return 0;
}
Learn Inheritance in C++
Comments
Post a Comment