#include <iostream>
using namespace std;
class A
{
public:
int x;
int y;
virtual int add() = 0;
};
class B
{
public:
int a;
int b;
virtual int sub() = 0;
};
class C : public A, public B
{
public:
C() { x = 1; y = 2; z = 3; a = 4; b = 5; c = 6; }
int c;
int z;
virtual int add() { return a + z; }
virtual int sub() { return c - x; }
int mul() { return b * y; };
};
class D : public A, public B
{
public:
D() { x = 1; y = 2; z = 3; a = 4; b = 5; c = 6; }
int c;
int z;
virtual int add() { return a + z; }
virtual int sub() { return x - c; } // sub 做的事情不一樣
int mul() { return b * y; };
};
int main()
{
A *p = new D; // 這裡視情況可能建立 C 或 D
cout << p->add() << endl;
cout << ((C *)p)->sub() << endl;
cout << ((C *)p)->mul() << endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKY2xhc3MgQQp7CnB1YmxpYzoKICAgIGludCB4OwogICAgaW50IHk7CiAgICB2aXJ0dWFsIGludCBhZGQoKSA9IDA7Cn07CgpjbGFzcyBCCnsKcHVibGljOgogICAgaW50IGE7CiAgICBpbnQgYjsKICAgIHZpcnR1YWwgaW50IHN1YigpID0gMDsKfTsKCmNsYXNzIEMgOiBwdWJsaWMgQSwgcHVibGljIEIKewpwdWJsaWM6CiAgICBDKCkgeyB4ID0gMTsgeSA9IDI7IHogPSAzOyBhID0gNDsgYiA9IDU7IGMgPSA2OyB9CiAgICBpbnQgYzsKICAgIGludCB6OwogICAgdmlydHVhbCBpbnQgYWRkKCkgeyByZXR1cm4gYSArIHo7IH0KICAgIHZpcnR1YWwgaW50IHN1YigpIHsgcmV0dXJuIGMgLSB4OyB9CiAgICBpbnQgbXVsKCkgeyByZXR1cm4gYiAqIHk7IH07Cn07CgpjbGFzcyBEIDogcHVibGljIEEsIHB1YmxpYyBCCnsKcHVibGljOgogICAgRCgpIHsgeCA9IDE7IHkgPSAyOyB6ID0gMzsgYSA9IDQ7IGIgPSA1OyBjID0gNjsgfQogICAgaW50IGM7CiAgICBpbnQg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