PTA 建立计算机类

6-5建立计算机 (10分)

界说一个简朴的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个工具,ram为RAM类的一个工具,cdrom为CDROM类的一个工具,界说并实现这个类,为以上的类编写组织和析构函数,注重使用类组合的头脑解决该问题,使得给出的主函数代码可以准确运行并获得给出的输出效果。

main函数

/* 请在这里填写谜底 */
在这里给出函数被挪用举行测试的例子。例如:
int main()
{
	COMPUTER cpt1(6,4.0,200,60,32);  //测试带参数组织
	cout<<"cpt1's parameter:"<<endl;
	cpt1.showinfo();
	cout<<"------------------------------"<<endl;
	COMPUTER cpt2; //测试不带参数组织
	cout<<"cpt2's parameter:"<<endl;
	cpt2.showinfo();
	cout<<"------------------------------"<<endl;
    COMPUTER cpt3(cpt1); //测试复制组织
	cout<<"cpt3's parameter:"<<endl;
	cpt3.showinfo();
	cout<<"------------------------------"<<endl;	
}

无输入

输出样例:

create a CPU!
create a RAM!
create a CDROM!
create a COMPUTER with para!
cpt1's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
create a CPU!
create a RAM!
create a CDROM!
no para to create a COMPUTER!
cpt2's parameter:
cpu parameter:
rank:1
frequency:2
voltage:100
ram parameter:
volumn:1 GB
cdrom parameter:
speed:16
------------------------------
create a CPU by copy!
create a RAM by copy!
create a CDROM by copy!
create a COMPUTER by copy!
cpt3's parameter:
cpu parameter:
rank:6
frequency:4
voltage:200
ram parameter:
volumn:60 GB
cdrom parameter:
speed:32
------------------------------
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!
destruct a COMPUTER!
destruct a CDROM!
desturct a RAM!
desturct a CPU!

剖析:

此题一共有三种情形的CPU:

  1. 传入实参时
  2. 不带参数时
  3. 拷贝其他工具时

先建立COMPUTER类

class COMPUTER{
public:
    COMPUTER(int i,int j,int k,int l,int m):cpu(i,j,k),ram(l),cdrom(m){
        cout<<"create a COMPUTER with para!"<<endl;
    }//传入实参时
    COMPUTER(){
        cout<<"no para to create a COMPUTER!"<<endl;
    }//不传实参
    COMPUTER(const COMPUTER &a):cpu(a.cpu),ram(a.ram),cdrom(a.cdrom){
        cout<<"create a COMPUTER by copy!"<<endl;
    }//拷贝时
    ~COMPUTER(){
        cout<<"destruct a COMPUTER!"<<endl;
    }
    void showinfo(){
        cpu.showinfo();
        ram.showinfo();
        cdrom.showinfo();
    }
private:
    CPU cpu;//i,j,k 三个数据成员
    RAM ram;//l 一个数据成员
    CDROM cdrom;   // m 一个数据成员
};

再来划分实现CPU、RAM、CDROM类

一、CPU类:

class CPU{
public:
    CPU(int i=1,int j=2,int k=100){//CPU有参数传入或者无参数传入时
        rank=i;
        frequency=j;
        voltage=k;
        cout<<"create a CPU!"<<endl;
    }
    CPU(const CPU &a){//拷贝时
        rank=a.rank;
        frequency=a.frequency;
        voltage=a.voltage;
        cout<<"create a CPU by copy!"<<endl;
    }
    ~CPU(){
        cout<<"desturct a CPU!"<<endl;
    }
    void showinfo(){
        cout<<"cpu parameter:"<<endl;
        cout<<"rank:"<<rank<<endl;
        cout<<"frequency:"<<frequency<<endl;
        cout<<"voltage:"<<voltage<<endl;
    }
private:
    int rank;
    int frequency;
    int voltage;
};

二、RAM类:

class RAM{
public:
    RAM(int i=1){
        volumn=i;
        cout<<"create a RAM!"<<endl;
    }
    RAM(const RAM &a){
        volumn=a.volumn;
        cout<<"create a RAM by copy!"<<endl;
    }
    ~RAM(){
        cout<<"desturct a RAM!"<<endl;
    }
    void showinfo(){
        cout<<"ram parameter:"<<endl;
        cout<<"volumn:"<<volumn<<" GB"<<endl;
    }
private:
    int volumn;
};

三、CDROM类:

SpringCloud系列之服务注册发现(Eureka)应用篇

class CDROM{
public:
    CDROM(int i=16){//有或无参数传入
        speed=i;
        cout<<"create a CDROM!"<<endl;
    }
    CDROM(const CDROM &a){//拷贝
        speed=a.speed;
        cout<<"create a CDROM by copy!"<<endl;
    }
    ~CDROM(){
        cout<<"destruct a CDROM!"<<endl;
    }
    void showinfo(){
        cout<<"cdrom parameter:"<<endl;
        cout<<"speed:"<<speed<<endl;
    }
private:
    int speed;
};

总结:

RAM、CDROM、CPU类结构相同

注重的是:在COMPUTER类中界说CPU、RAM、CDROM类的工具,而且对这些工具的函数引用

然则run stop函数是什么鬼?希望大神告诉我

挖个坑,过几天来填

原创文章,作者:2d28新闻网,如若转载,请注明出处:https://www.2d28.com/archives/3047.html