Objects and classes - Construction class CPU, RAM-based, CD_ROM class, COMPUTER Class

Problem Description:
Construction Computers: the CPU type, the RAM type, the CD_ROM class, COMPUTER class, and requirements: 1. The private data members declared cpu, ram, cdrom, declared public member functions run, stop, may output a message therein. 2. declare the object of a Computer class in main () function, call its member functions.


1. Declare a class CPU:

#include<iostream>
using namespace std;
enum CPU_Rank{ P1 = 1, P2, P3, P4, P5, P6, P7 };


//声明一个CPU类
class CPU{
private:
	CPU_Rank rank;  //CPU等级
	int frequency;  //CPU主频
	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_Rank GetRank()const { return rank; }      // 外部接口函数,用于设置私有成员rank
	int GetFrequency()const { return frequency; } // 外部接口函数
	float GetVoltage()const { return voltage; }   // 外部接口函数
  
	void SetRank(CPU_Rank r){ rank = r; }         // 私有数据成员的接口,用于获取私有数据成员rank的值
	void SetFrequency(int f) { frequency = f; }   // 私有数据成员的接口
	void SetVoltage(float v) { voltage = v; }     // 私有数据成员的接口
 
	void Run(){ cout << "CPU开始运行!" << endl; }
	void Stop(){ cout << "CPU停止运行!" << endl; }
};

2. Declare a RAM categories:

//声明一个RAM类
enum RAM_Type{DDR2=2,DDR3,DDR4};
class RAM{
private:
	enum RAM_Type type;
	unsigned int frequency;  //MHz
	unsigned int size;  //GB
public:
	RAM(RAM_Type t, unsigned int f, unsigned int s)
	{
		type = t;
		frequency = f;
		size = s;
		cout << "构造了一个RAM" << endl;
	}
	~RAM(){ cout << "析构了一个RAM" << endl; }
	RAM_Type GetType()const { return type; }               // 外部接口函数
	unsigned int GetFrequency()const { return frequency; }
	unsigned int GetSize()const { return size; }
 
	void SetType(RAM_Type t){ type = t; }                  // 私有数据成员的接口
	void SetFrequency(unsigned int f){ frequency = f; }
	void SetSize(unsigned int s){ size = s; }

	void Run(){ cout << "RAM开始运行!" << endl; }
	void Stop(){ cout << "RAM停止运行!" << endl; }
};

3. Declare a CDROM class:

//声明一个CDROM类
enum CDROM_Interface{SATA,USB};
enum CDROM_Install_type{external,built_in};
class CD_ROM{
private:
	enum CDROM_Interface interface_type;
	enum CDROM_Install_type install_type;
	unsigned int cache_size;  //MB
 
public:
	CD_ROM(CDROM_Interface i, unsigned int s, CDROM_Install_type it)
	{
		interface_type = i;
		cache_size = s;
		install_type = it;
		cout << "构造了了一个CD_ROM!" << endl;
	}
 
	~CD_ROM(){ cout << "析构了一个CD_ROM!" << endl; }
	CDROM_Interface GetInterfaceType()const { return interface_type; }
	CDROM_Install_type GetInstallType()const{ return install_type; }
	unsigned int GetCacheSize()const { return cache_size; }
 
	void SetInterfaceType(CDROM_Interface i){ interface_type = i; }
	void SetInstallType(CDROM_Install_type it){ install_type = it; }
	void SetCacheSize(unsigned int s){ cache_size = s; }
 
	void Run(){ cout << "CD_ROM开始运行!" << endl; }
	void Stop(){ cout << "CD_ROM停止运行!" << endl; }
};



//构建COMPUTER类
class COMPUTER{
private:
	CPU my_cpu;
	RAM my_ram;
	CD_ROM my_cdrom;
	unsigned int storage_size;  //GB
	unsigned int bandwidth;     //MB

public:
	COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int ss, unsigned int bw);
	~COMPUTER(){ cout << "析构了一个COMPUTER" << endl; }
 
	void Run()
	{
		my_cpu.Run();
		my_ram.Run();
		my_cdrom.Run();
		cout << "COMPUTER开始运行!" << endl;
	}
	void Stop()
	{
		my_cpu.Stop();
		my_ram.Stop();
		my_cdrom.Stop();
		cout << "COMPUTER停止运行!" << endl;
	}
};

//构造函数写在外部,要加上作用域的限定符
COMPUTER::COMPUTER(CPU c, RAM r, CD_ROM cd, unsigned int ss, unsigned int bw) :my_cpu(c), my_ram(r), my_cdrom(cd)
{                       // 5个参数中,前面3个是内嵌对象,因此冒号后面给出三个初始化列表,让这3个内嵌对象分别调用他们各自类的拷贝构造函数(这个拷贝构造函数是系统自定义的),来完成这3个内嵌对象的初始化
	storage_size = ss;  // 简单数据成员,在圆括号中通过赋值语句来完成初始化
	bandwidth = bw;

	cout << "构造了一个COMPUTER!" << endl;
}

The main function:

int main()
{
	CPU a(P6, 300, 2.8f);
	a.Run();
	a.Stop();
	cout << "******************" << endl;
 
	RAM b(DDR3, 1600, 8);
	b.Run();
	b.Stop();
	cout << "******************" << endl;
 
	CD_ROM c(SATA, 2, built_in);
	c.Run();
	c.Stop();
	cout << "******************" << endl;
 
	COMPUTER my_computer(a, b, c, 128, 10);
	cout << "******************" << endl;
 
	my_computer.Run();
	my_computer.Stop();
	return 0;
}

The results are:

Here Insert Picture Description
Description:
  When the constructor Computer, five parameters, the first three are embedded object, thus giving three initialization list after the colon, so that the three objects are embedded copy constructor to call their respective class (the copy a constructor is a system-defined), to complete the initialization of the three embedded object.


Video address

Published 255 original articles · won praise 28 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43283397/article/details/104429268