#include<iostream>
#include<string>
using namespace std;
//不同的抽象类
class Memory
{
public:
virtual void storage() = 0;
};
class Cpu
{
public:
virtual void caculate() = 0;
};
class VideoCard
{
public:
virtual void display() = 0;
};
//电脑类
class Computer
{
public:
Computer(Cpu*cpu,Memory*mem,VideoCard*vc)
{
m_cpu = cpu;
m_mem = mem;
m_vc = vc;
}
//提供工作的函数
void work()
{
//让连接工作起来,调用接口
m_cpu->caculate();
m_vc->display();
m_mem->storage();
}
~Computer()
{ //释放cpu零件
if (m_cpu != NULL)
{
delete m_cpu;
m_cpu = NULL;
}
//释放内存条零件
if (m_mem != NULL)
{
delete m_mem;
m_mem = NULL;
}
//释放显卡零件
if (m_vc != NULL)
{
delete m_vc;
m_vc = NULL;
}
}
private:
Cpu* m_cpu;
Memory* m_mem;
VideoCard* m_vc;
};
//intel厂商
class IntelCpu :public Cpu
{
public:
virtual void caculate()
{
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;
}
};
//lenovo厂商
class LenovoMemory:public Memory
{
public:
virtual void storage()
{
cout << "Lenovo的内存条开始存储了" << endl;
}
};
class LenovoVideoCard :public VideoCard
{
public:
virtual void display()
{
cout << "Lenovo的显卡开始显示了" << endl;
}
};
class LenovolCpu :public Cpu
{
public:
virtual void caculate()
{
cout << "Lenovo的cpu开始计算了" << endl;
}
};
void test01()
{
//创建电脑零件
cout << "第一台电脑开始工作" << endl;
Memory* intelMem = new IntelMemory;
Cpu* intelCpu = new IntelCpu;
VideoCard* intelCard = new IntelVideoCard;
Computer* computer1 = new Computer(intelCpu, intelMem, intelCard);
computer1->work();
delete computer1;
cout << "------------------------------------------------------------"<<endl;
cout << "第二台电脑开始工作" << endl;
Memory* lenovoMem = new LenovoMemory;
Cpu* lenovoCpu = new LenovolCpu;
VideoCard* lenovoVideoCard = new LenovoVideoCard;
Computer* computer2 =new Computer(lenovoCpu,lenovoMem,lenovoVideoCard);
computer2->work();
delete computer2;
cout << "------------------------------------------------------------" << endl;
cout << "第三台电脑开始工作" << endl;
Memory* lenovoMem1 = new LenovoMemory;
Cpu* lenovoCpu1 = new LenovolCpu;
VideoCard* lenovoVideoCard1 = new LenovoVideoCard;
Computer* computer3 = new Computer(lenovoCpu1, lenovoMem1, lenovoVideoCard1);
computer3->work();
delete computer3;
cout << "------------------------------------------------------------" << endl;
}
int main() {
test01();
}