#include <stdio.h>
class IA {
public:
virtual int buy() = 0;
void echoA() {
printf("echo A\n");
}
};
class IB {
public:
virtual int buy() = 0;
void echoB() {
printf("echo B\n");
}
};
class C : public IA, public IB {
public:
virtual int buy() {
printf("buy whatever\n");
return 0;
}
};
int main() {
C c;
c.buy();
c.echoA();
c.echoB();
IA *a = &c;
a->buy();
IB *b = &c;
b->buy();
}
上面的代码能编译通过吗?C 继承了 IA 和 IB,如何实现两个 buy 方法呢?
动手看看编译运行结果:
[root] $g++ test.cpp
[root] $./a.out
buy whatever
echo A
echo B
buy whatever
buy whatever
Not as expected, right? Haha, Java has similar behaviour.