PTA 创建计算机类

2019/04/10 10:10
阅读数 16

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类:

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函数是什么鬼?希望大神告诉我

挖个坑,过几天来填

原文出处:https://www.cnblogs.com/am6a6/p/12589044.html

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部