PTA 6-5 创建计算机(一道巨坑的题)

本文详细描述了一个包含CPU、RAM和CDROM组件的简单Computer类的实现,包括有参和无参构造函数、拷贝构造函数以及析构函数。特别强调了类组合思想的应用以及输出中关于GB单位的处理。
摘要由CSDN通过智能技术生成

题目要求:

定义一个简单的Computer类,有数据成员芯片(cpu)、内存(ram)、光驱(cdrom)等等,有两个公有成员函数run、stop。cpu为CPU类的一个对象,ram为RAM类的一个对象,cdrom为CDROM类的一个对象,定义并实现这个类,为以上的类编写构造和析构函数,注意使用类组合的思想解决该问题,使得给出的主函数代码可以正确运行并得到给出的输出结果。

坑点:

1.单位GB

2.COMPUTER和CDROM的析构函数的输出是destruct,而CPU和RAM的析构函数输出是desturct

代码实现:

#include <iostream>
using namespace std;
class CPU
{
private:
    int rank;
    int frequency;
    int voltage;
public:
    //有参构造函数:
    CPU(int i = 1, int j = 2, int k = 100)
    {
        this->rank = i;
        this->frequency = j;
        this->voltage = k;
        cout << "create a CPU!" << endl;       
    }
  
    //拷贝构造函数:
    CPU(const CPU &p)
    {
        this->rank = p.rank;
        this->frequency = p.frequency;
        this->voltage = p.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;
    }
    
};

class RAM
{
private:
    int volumn;
public:
    RAM(int vol = 1)
    {
        this->volumn = vol;
        cout << "create a RAM!" << endl;       
    }
    RAM(const RAM &r)
    {
        this->volumn = r.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;
    }
   
};

class CDROM
{
private:
    int speed;
public:
    CDROM(int c = 16)
    {
        this->speed = c;
        cout << "create a CDROM!" << endl;
    }
    CDROM(const CDROM &a)
    {
        this->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;
    }
    
};

class COMPUTER
{
private:
    CPU cpu;
    RAM ram;
    CDROM cdrom;
public:
    COMPUTER(int r,int f,int v,int l,int s):cpu(r,f,v),ram(l),cdrom(s)
    {
        cout << "create a COMPUTER with para!" << endl;
    }
    COMPUTER()
    {
        cout << "no para to create a COMPUTER!" << endl;
    }
    COMPUTER(const COMPUTER &p):cpu(p.cpu),ram(p.ram),cdrom(p.cdrom)
    {
        cout << "create a COMPUTER by copy!" << endl;
    }
    ~COMPUTER()
    {
        cout << "destruct a COMPUTER!" << endl;
    }
    void showinfo()
    {
        cpu.showinfo();
        ram.showinfo();
        cdrom.showinfo();
    }
};

这题真是有病(小声吐槽)(狗头保命)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值