类和对象 多态 案例3电脑组装

#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();
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值