#include
using namespace std;
class CPU {//cpu类
public:
virtual void caculate() = 0;
};
class GPU {//gpu类
public:
virtual void vedio_caculate() = 0;
};
class Intel :public CPU {//Intel
public:
void caculate() {
cout << "is Intel's CPU" << endl;
}
};
class NVIDIA :public GPU {//NVIDIA
public:
void vedio_caculate() {
cout << "is NVIDIA's GPU" << endl;
}
};
class AMD :public CPU, public GPU {//AMD品牌的cpu和gpu
public:
void CPU::caculate() {
cout << "is AMD's CPU" << endl;
}
void GPU::vedio_caculate() {
cout << "is AMD's GPU" << endl;
}
};
class Computer {
public:
Computer(CPU* p1, GPU* p2) :cpu(p1), gpu(p2) { ; }//构造函数赋值
void show() {//展示电脑配置
cout << "Computer:" << endl;
cpu->caculate();
gpu->vedio_caculate();
}
~Computer() {//析构释放内存空间
if ((cpu != NULL) && (gpu != NULL)) {
delete cpu;
delete gpu;
}
cpu = NULL;
gpu = NULL;
}
public:
CPU* cpu;//电脑cpu
GPU* gpu;//电脑gpu
};
void test() {
//Computer myComputer_0(new Intel, new AMD);//这里AMD类的GPU部分出错
Computer myComputer_1(new AMD, new NVIDIA);//AMD类的CPU部分没问题
//把class AMD :public CPU, public GPU
//改为class AMD :public GPU, public CPU
//继承顺序对调后就是AMD类的CPU部分报错了
}
int main() {
test();
return 0;
}
多继承请用virutal 继承