multiple-inh/ambiguities/amb-3.cc - Involving polomorphism - problem solved. | Lecture 5 - slide 18 : 40 Program 4 |
// The solution to the problem just encountered. The function members // named operation in the classes A and B must be virtual. #include <iostream> #include <string> using namespace std; class A { public: int data; virtual int operation(){ cout << "A: operation" << endl; return data; } }; class B { public: int data; virtual int operation(){ cout << "B: operation" << endl; return data; } }; class C: public A, public B{ public: int operation() override { cout << "C: operation" << endl; int r1 = A::operation(), r2 = B::operation(); return r1 + r2; } }; int f(A* obj){ int res = obj->operation(); return res; } int main(){ A *obj = new C(); f(obj); // OUTPUT: // C: operation // A: operation // B: operation }