4.7.6电脑组装

4.7.6电脑组装


#include <iostream>
using namespace std;
class CPU
{
public:
	virtual void caculate() = 0;
};
class videocard
{
public:
	virtual void display() = 0;
};
class memory
{
public:
	virtual void storage() = 0;
};

class computer
{
public:
	computer(CPU *cpu, videocard * vc, memory *mem)
	{
		m_cpu = cpu;
		m_vc = vc;
		m_mem = mem;
	}
	void work()
	{
		m_cpu->caculate();
		m_vc->display();
		m_mem->storage();
	}
private:
	CPU *m_cpu;
	videocard * m_vc;
	memory *m_mem;
};
class intelcpu :public CPU
{public:
	virtual void caculate()
	{
		cout << "intel__cpu" << endl;
	}

};
class intelvideocard :public videocard
{
public:
	virtual void caculate()
	{
		cout << "intel__videocard" << endl;
	}

};
class intelmemory :public memory
{
public:
	virtual void caculate()
	{
		cout << "intel__memory" << endl;
	}

};
class lenovocpu :public CPU
{
public:
	virtual void caculate()
	{
		cout << "lenovo__cpu" << endl;
	}

};
class lenovovideocard :public videocard
{
public:
	virtual void caculate()
	{
		cout << "lenovo__videocard" << endl;
	}

};
class lenovomemory :public memory
{
public:
	virtual void caculate()
	{
		cout << "lenovo__memory" << endl;
	}

};
void test01()
{
	CPU *intelcpu = new lenovocpu;
	videocard *intelcard = new lenovovideocard;
	memory *intelmemo = new intelmemory;
	computer *c1 = new computer(intelcpu, intelcard, intelmemo);
	c1->work();
}
int main()
{
	test01();


	system("pause");
	return 0;
}

4.7.5虚析构和纯虚虚构


#include <iostream>
using namespace std;
#include<string>
//虚析构,纯虚析构
class Animal
{
public:
	Animal()
	{
		cout << "Animal" << endl;
	}


	virtual void speak() = 0;
	//纯虚析构
	virtual ~Animal()
	{
		cout << "~Animal" << endl;
	}
};

class cat :public Animal
{
public:
	cat(string name)
	{
		cout << "cat()" << endl;
		m_name = new string(name);
	}
	virtual void speak()
	{
		cout << *m_name<<"  cat speak" << endl;
	}
	string *m_name;
	~cat()
	{
		if (m_name != NULL)
		{
			cout << "~cat()" << endl;
			delete m_name;
			m_name = NULL;
		}
	}
};
void test01()
{
	Animal *ani = new cat("tom");
	ani->speak();
	delete ani;

}

int main4_7_5()
{
	test01();


	system("pause");
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

cdbycd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值