fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Base {
  5. Base operator+(const Base& other) {
  6. return add(other);
  7. }
  8. protected:
  9. virtual Base add(const Base& other) {
  10. cout << "Adding in Base's code." << endl;
  11. return Base();
  12. }
  13. };
  14.  
  15. struct Derived : public Base {
  16. protected:
  17. virtual Base add(const Base& other) {
  18. cout << "Adding in Derived's code." << endl;
  19. return Derived();
  20. }
  21. };
  22.  
  23. int main() {
  24. Base b1;
  25. Base b2;
  26. Derived d1;
  27. Derived d2;
  28. Base res;
  29. res = b1+b2;
  30. res = b1+d2;
  31. res = d1+b2;
  32. res = d1+d2;
  33. return 0;
  34. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
Adding in Base's code.
Adding in Base's code.
Adding in Derived's code.
Adding in Derived's code.