计算机包含CPU和硬盘。请设计Computer、CPU、Disk类。并满足以下测试
int main()
{
string cpuType, diskType;
double frequency, mount;
cin >> cpuType >> frequency >> diskType >> mount;
CPU cpu(cpuType, frequency);
Disk disk(diskType, mount);
Computer computer(cpu, disk);
computer.Print();
return 0;
}
输入
cpu类型 cpu主频
disk类型 disk容量
输出
见测试样例
样例输入1 复制
i7 2.9
ST 2
样例输出1
The computer has a cpu and a disk.
CPU type: i7, CPU frequency: 2.9 GHz
disk type: ST, disk mount: 2 T
怎么实现这个问题?
class CPU
{
public:
CPU();
CPU(string CpuType,double frequency);
};
class Disk
{
public:
Disk();
Disk(string DiskType,double mount);
};
class Computer
{
public:
Computer();
Computer(CPU cpu,Disk diks);
};
来一个简单的
class CPU
{
public:
CPU() {}
CPU(string _type, double _frenquency) :cpuType(_type), frenquency(_frenquency) {}
string gettype(void) { return this->cpuType; }
double getfreq(void) { return this->frenquency; }
private:
string cpuType;
double frenquency;
};
class Disk
{
public:
Disk() {}
Disk(string _type, double _mount) :diskType(_type), mount(_mount) {}
string gettype(void) { return this->diskType; }
double getmount(void) { return this->mount; }
private:
string diskType;
double mount;
};
class Computer
{
public:
Computer() {}
Computer(CPU _cpu, Disk _disk) :C_CPU(_cpu), C_Disk(_disk) {}
void print(void) {
cout << "The computer has a cpu and a disk." << endl;
cout << "CPU type : " << C_CPU.gettype() << ", CPU frequency : " << C_CPU.getfreq() << " GHz" << endl;
cout << "disk type : " << C_Disk.gettype() << ", disk mount : " << C_Disk.getmount() << " T" << endl;
}
private:
CPU C_CPU;
Disk C_Disk;
};