实验3.2 定义一个简单的Computer类

题目

定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类。

AC的C++代码如下:

#include <iostream> 
using namespace std; 
enum CPU_rank {P1=1,P2,P3,P4,P5,P6,P7}; 
class CPU 
{   
private: 
	CPU_rank rank; 
	int frequency; 
	float voltage; 
public: 
    CPU (CPU_rank r, int f, float v) 
	{    
		rank = r; 
  		frequency = f; 
  		voltage = v; 
  		cout << "构造了一个CPU!" << endl; 
 	} 
	CPU() {   cout << "构造了一个CPU!" << endl; };   
	~CPU () {    cout << "析构了一个CPU!" << endl; } 
 	void Run() {cout << "CPU开始运行!" << endl; } 
 	void Stop() {cout << "CPU停止运行!" << endl; } 
}; 

class RAM 
{    
public: 
    RAM () {   cout << "构造了一个RAM!" << endl;  }  
	~RAM (){   cout << "析构了一个RAM!" << endl; } 
   	void Run() {cout << "RAM开始运行!" << endl; } 
 	void Stop() {cout << "RAM停止运行!" << endl; } 
}; 

class CDROM 
{   
public: 
    CDROM (){ cout << "构造了一个CDROM!" << endl; }   
	~CDROM () { cout << "析构了一个CDROM!" << endl;  } 
   	void Run() {cout << "CDROM开始运行!" << endl; } 
 	void Stop() {cout << "CDROM停止运行!" << endl; } 
}; 

class computer 
{   
private: 
 	CPU cpu; 
 	RAM ram; 
 	CDROM cdrom; 
public: 
 	computer() {cout << "构造了一个computer!" << endl; }
	~computer () { cout << "析构了一个computer!" << endl; } 
   	void Run()  
 	{   
	 	cout << "computer开始运行!" << endl; 
  		cpu.Run(); 
  		ram.Run(); 
 	} 
 	void Stop()  
 	{   ram.Stop(); 
  		cpu.Stop(); 
  		cout << "computer停止运行!" << endl;  
 	} 
}; 

int main() 
{    
	computer a; 
 	a.Run(); 
 	a.Stop(); 
 	return 0;
} 
  • 29
    点赞
  • 125
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值