#include <iostream>
using namespace std;
struct Base {
Base operator+(const Base& other) {
return add(other);
}
protected:
virtual Base add(const Base& other) {
cout << "Adding in Base's code." << endl;
return Base();
}
};
struct Derived : public Base {
protected:
virtual Base add(const Base& other) {
cout << "Adding in Derived's code." << endl;
return Derived();
}
};
int main() {
Base b1;
Base b2;
Derived d1;
Derived d2;
Base res;
res = b1+b2;
res = b1+d2;
res = d1+b2;
res = d1+d2;
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKc3RydWN0IEJhc2UgewoJQmFzZSBvcGVyYXRvcisoY29uc3QgQmFzZSYgb3RoZXIpIHsKCQlyZXR1cm4gYWRkKG90aGVyKTsKCX0KcHJvdGVjdGVkOgogICAgdmlydHVhbCBCYXNlIGFkZChjb25zdCBCYXNlJiBvdGhlcikgewogICAgCWNvdXQgPDwgIkFkZGluZyBpbiBCYXNlJ3MgY29kZS4iIDw8IGVuZGw7CiAgICAJcmV0dXJuIEJhc2UoKTsKICAgIH0KfTsKCnN0cnVjdCBEZXJpdmVkIDogcHVibGljIEJhc2Ugewpwcm90ZWN0ZWQ6CiAgICB2aXJ0dWFsIEJhc2UgYWRkKGNvbnN0IEJhc2UmIG90aGVyKSB7CiAgICAJY291dCA8PCAiQWRkaW5nIGluIERlcml2ZWQncyBjb2RlLiIgPDwgZW5kbDsKICAgIAlyZXR1cm4gRGVyaXZlZCgpOwogICAgfQp9OwoKaW50IG1haW4oKSB7CglCYXNlIGIxOwoJQmFzZSBiMjsKCURlcml2ZWQgZDE7CglEZXJpdmVkIGQyOwoJQmFzZSByZXM7CglyZXMgPSBiMStiMjsKCXJlcyA9IGIxK2QyOwoJcmVzID0gZDErYjI7CglyZXMgPSBkMStkMjsKCXJldHVybiAwOwp9