Back to slide -- Keyboard shortcut: 'u'  previous -- Keyboard shortcut: 'p'  next -- Keyboard shortcut: 'n'          multiple-inh/ambiguities/amb-3-problems.cc - Involving polomorphism - but there is a problem.Lecture 5 - slide 18 : 40
Program 3

// We introduce a function called operation in C which activates the
// operation in both A and B. We do NOT get the intended results. 
// Why?  The methods are not virtual.

#include <iostream>
#include <string>

using namespace std;

class A {
public:
  int data;

  int operation(){
     cout << "A: operation" << endl;
     return data;
  }
};


class B {
public:
  int data;

  int operation(){
     cout << "B: operation" << endl;
     return data;
  }
};


class C: public A, public B{
public: 
  int operation(){   
     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(); // Polymorphism:
                    // A variable of static type A refers to a C-object
  f(obj);           // ACTUAL OUTPUT:
                    //  A: operation. 
                    // WANTED AND EXPECTED OUTPUT:
                    //  C: operation. 
                    //  A: operation. 
                    //  B: operation. 
                    // What is the problem?  The methods are not virtual.
}