给我一台计算机吧!

Description

CPU主频和主存容量是衡量一台计算机性能主要指标。请定义:

  1. CPU类:只有一个int类型的数据成员以表示其主频,并请根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

  2. Memory类:只有一个int类型的数据成员以表示其容量,并请根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

  3. Computer类:

(1)三个数据成员分别为CPU的对象、Memory的对象和一个字符串(表示该计算机属于谁的)。

(2)根据输出和给定的main()函数编写必要的成员函数(包括构造函数)。

(3)void show()方法,用于按照样例输出格式输出该计算机的信息。

Input

输入有2行。每行包括2个整数和1个字符串,分别表示CPU的主频、内存容量和计算机主人姓名。

Output

见样例。

Sample Input
2 1000 Zhang
4 2000 Li
Sample Output
This is Zhang’ computer with CPU = 2GHz, memory = 1000MB.
This is Li’ computer with CPU = 4GHz, memory = 2000MB.
HINT

Append Code
append.cc,

int main()
{
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}

AC代码一(这个代码不全,因为没有写拷贝构造函数,为什么还能运行呢?是因为编译器会自动生成一个默认的拷贝构造函数)

#include <iostream>
#include <iomanip>
using namespace std;
class CPU
{
    int _a;
public:
    CPU(int a):_a(a){}
    int getC(int a){return _a=a;}
    int get(){return _a;}
};
class Memory
{
    int _b;
public:
    Memory(int b):_b(b){}
    int getM(int m){return _b=m;}
    int get(){return _b;}
};
class Computer
{
    string _i;
    CPU _j;
    Memory _k;
public:
    Computer(CPU j,Memory k,string i):_j(j),_k(k),_i(i){}
    Computer(int c,int m,string n):_j(_j.getC(c)),_k(_k.getM(m)),_i(n){}//注意这一行是怎么赋值的;
    void show(){cout<<"This is "<<_i<<"' computer with CPU = "<<_j.get()<<"GHz, memory = "<<_k.get()<<"MB."<<endl;}

};
int main()
{
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}

AC代码二(这个是完整的代码,写了拷贝构造函数)在注释那一行调用了拷贝构造函数

#include <iostream>

using namespace std;
class CPU
{
private:
    int _c;
public:
    CPU(int c):_c(c){}
    int getC(){return _c;}
    CPU(const CPU& p):_c(p._c){}
    ~CPU(){}
};
class Memory
{
private:
    int _c;
public:
    Memory(int c):_c(c){}
    Memory(const Memory& p):_c(p._c){}
    int getM(){return _c;}
    ~Memory(){}
};
class Computer
{
private:
    CPU p1;
    Memory p2;
    string name;
public:
    Computer(CPU cpu,Memory me,string na):p1(cpu),p2(me),name(na){}//这里会调用拷贝构造函数
    void show(){cout<<"This is "<<name<<"’ computer with CPU = "<<p1.getC()<<"GHz, memory = "<<p2.getM()<<"MB."<<endl;}
};
int main()
{
    int c, m;
    string n;
    cin>>c>>m>>n;
    CPU cpu(c);
    Memory mem(m);
    Computer com1(cpu, mem, n);
    cin>>c>>m>>n;
    Computer com2(c, m, n);
    com1.show();
    com2.show();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值