组装一台电脑
//组装使用电脑//CPU抽象类class CPU{public://抽象计算virtual void calculate() = 0;};//GPU抽象类class GPU{public://抽象显示virtual void dispaly() = 0;};//Memory抽象类class Memory{public://抽象存储virtual void storage() = 0;};//电脑类class Computer{public:Computer(CPU* cpu, GPU* gpu, Memory* mem) :m_cpu(cpu), m_gpu(gpu), m_mem(mem){}//提供工作的函数void work(){//让零件工作起来,调用接口m_cpu->calculate();m_gpu->dispaly();m_mem->storage();}~Computer(){if (NULL != m_cpu){delete m_cpu;m_cpu = nullptr;}if (NULL != m_gpu){delete m_gpu;m_gpu = nullptr;}if (NULL != m_mem){delete m_mem;m_mem = nullptr;}}private:CPU* m_cpu; //cpu零件指针GPU* m_gpu; //gpu零件指针Memory* m_mem; //内存条零件指针};//具体厂商//Intel厂商class IntelCPU :public CPU{public:void calculate(){cout << "Intel CPU开始计算" << endl;}};class LenovoCPU :public CPU{public:void calculate(){cout << "Lenovo CPU开始计算" << endl;}};class IntelGPU :public GPU{public:void dispaly(){cout << "Intel GPU开始显示" << endl;}};class LenovoGPU :public GPU{public:void dispaly(){cout << "Lenovo GPU开始显示" << endl;}};class IntelMemory :public Memory{public:void storage(){cout << "Intel Memory开始存储数据" << endl;}};class LenovoMemory :public Memory{public:void storage(){cout << "Lenovo Memory开始存储数据" << endl;}};void test01(){//第一台零件CPU* intelCpu = new IntelCPU;GPU* intelGpu = new IntelGPU;Memory* intelMem = new IntelMemory;//创建第一台电脑Computer* computer1 = new Computer(intelCpu, intelGpu, intelMem);computer1->work();delete computer1;cout << "--------------------------" << endl;//创建第二台电脑Computer* computer2 = new Computer(new LenovoCPU, new LenovoGPU, new LenovoMemory);computer2->work();delete computer2;}int main(void){test01();return 0;}
