小记一则(3)

#include<iostream>
using namespace std;
class CPU {
private:
	int rank;
	int frequency;
	float voltage;
public:
	void run() { cout << "CPU开始运行" << endl; }
	void stop() { cout << "CPU停止运行" << endl; }
	~CPU() { cout << "CPU析构函数被调用" << endl; }
	CPU(int r, int f, float v)
	{
		this->rank = r;
		this->frequency = f;
		this->voltage = v;

	}

};
class RAM {
private:
	int size_ram;
public:
	void run() { cout << "RAM开始运行" << endl; }
	void stop() { cout << "RAM停止运行" << endl; }
	~RAM() { cout << "RAM析构函数被调用" << endl; }
	RAM(int s)
	{
		this->size_ram = s;
	}

};
class CDROM {
private:
	int size_cdrom;
public:
	void run() { cout << "CDROM开始运行" << endl; }
	void stop() { cout << "CDROM停止运行" << endl; }
	~CDROM() { cout << "CDROM析构函数被调用" << endl; }
	CDROM(int s)
	{
		this->size_cdrom = s;
	}
};
class Computer {
private:
	CPU cpu;
	CDROM cdrom;
	RAM ram;
public:
	Computer(CPU c, CDROM cd, RAM r):cpu(c),cdrom(cd),ram(r)
	{
		cout << "Computer构造函数被调用" << endl;
	}
	~Computer() { cout << "Computer析构函数被调用" << endl; }

	void run() {
		cout << "Computer开始运行" << endl;
		cpu.run();
		cdrom.run();
		ram.run();
	}
	void stop() {
		cpu.stop();
		cdrom.stop();
		ram.stop();
		cout << "Computer停止运行" << endl;
	}
};
int main()
{
	CPU cp(2, 100, 200);
	CDROM cdr(128);
	RAM r(256);
	Computer c(cp, cdr, r);
	c.run();
	c.stop();
	return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值