电脑组装案例

#include<iostream>
using namespace std;
#include<string>
class CPU {
public:
virtual void calculate() = 0;
};
class VideoCard {
public:
virtual void display() = 0;
};
class Memory {
public:
virtual void storage() = 0;
};
class Computer {
public:
Computer(CPU* x_cpu, VideoCard* x_vc, Memory* x_mem) {
cpu = x_cpu;
vc = x_vc;
mem = x_mem;
}
void work() {
cpu->calculate();
vc->display();
mem->storage();
}
~Computer() {
if (cpu != NULL) {
delete cpu;
cpu = NULL;
}
if (vc != NULL) {
delete vc;
vc = NULL;
}
if (mem != NULL) {
delete mem;
mem = NULL;
}
}
private:
CPU * cpu;
VideoCard * vc;
Memory * mem;
};
class LenovoCPU:public CPU{
public:
virtual void calculate() {
cout << "Lenovo的CPU开始计算了!" << endl;
}
};
class LenovoVideoCard :public VideoCard {
public:
virtual void display() {
cout << "Lenovo的显卡开始计算了!" << endl;
}
};
class LenovoVideoMemory :public Memory {
public:
virtual void storage() {
cout << "Lenovo的内存条开始计算了!" << endl;
}
};
class IntelCPU :public CPU {
public:
virtual void calculate() {
cout << "Intel的CPU开始计算了!" << endl;
}
};
class IntelVideoCard :public VideoCard {
public:
virtual void display() {
cout << "Intel的显卡开始计算了!" << endl;
}
};
class IntelMemory :public Memory {
public:
virtual void storage() {
cout << "Intel的内存条开始计算了!" << endl;
}
};
int main() {
CPU * intelCpu = new IntelCPU;
VideoCard * intelCard = new IntelVideoCard;
Memory * intelMem = new IntelMemory;
Computer * computer1 = new Computer(intelCpu, intelCard, intelMem);
delete computer1;
cout << "******************************************" << endl;
cout << "第二台电脑开始组装工作!";
Computer * computer2 = new Computer(new LenovoCPU, new IntelVideoCard, new IntelMemory);
delete computer2;
cout << "******************************************" << endl;
cout << "第三台电脑开始组装工作!";
Computer * computer3 = new Computer(new IntelCPU, new LenovoVideoCard, new IntelMemory);
delete computer3;
system("pause");
return 0;
}