C++ 代码优化:修复构造函数、属性初始化和输出语句问题
#pragma once
#include
enum CPU_Rank { P1 = 1, P2, P3, P4, P5, P6, P7 };
class CPU { private: CPU_Rank rank; int CPU_frequency; double CPU_voltage; public: CPU(int newRank, int newFrequency, int newVoltage) :rank((CPU_Rank)newRank), CPU_frequency(newFrequency), CPU_voltage(newVoltage) {} CPU(int newFrequency) :CPU_frequency(newFrequency) {} ~CPU() { cout << 'Good bye!' << endl; } CPU(const CPU& p) :rank(p.rank), CPU_frequency(p.CPU_frequency), CPU_voltage(p.CPU_voltage) {} CPU_Rank outRank() { return rank; } int outF() { return CPU_frequency; } double outV() { return CPU_voltage; } };
class RAM { private: string name; int RAM_frequency; int RAM_capacity; public: RAM() :name(''), RAM_frequency(0), RAM_capacity(0) {} RAM(string Name, int a, int b) :name(Name), RAM_frequency(a), RAM_capacity(b) {} RAM(int a) :RAM_capacity(a) {} ~RAM() { cout << 'Good bye!' << endl; } string Pname() { return name; } int P_ram_frequency() { return RAM_frequency; } int P_ram_capacity() { return RAM_capacity; } };
class CDROM { private: string Cname; int speed; public: CDROM() :Cname(''), speed(0) {} CDROM(string CName, int Cspeed) :Cname(CName), speed(Cspeed) {} CDROM(string M) :Cname(M) {} ~CDROM() { cout << 'Good bye!' << endl; } string P_Cname() { return Cname; } int Pspeed() { return speed; } };
class Computer :public CPU, public RAM, public CDROM { public: Computer(int rank, int frequency, int voltage, string* name, int ram_frequency, int ram_capacity, string* cname, int speed) : CPU(rank, frequency, voltage), RAM(*name, ram_frequency, ram_capacity), CDROM(cname, speed) {} Computer(int ram_capacity, int cpu_frequency, string cname) :CPU(cpu_frequency), RAM(ram_capacity), CDROM(*cname) {} void run() { cout << 'Computer开始运行!' << endl; } void stop() { cout << 'Computer停止运行!' << endl; } void output1() { cout << '这是一台组装机:' << endl; cout << 'CPU:' << endl; cout << '等级:' << outRank() << endl << '频率:' << outF() << endl << '电压:' << outV() << endl; cout << 'RAM:' << endl; cout << '品牌:' << Pname() << endl << '主频:' << P_ram_frequency() << endl << '容量:' << P_ram_capacity() << endl; cout << 'CDROM:' << endl; cout << '品牌:' << P_Cname() << endl << '传输速率:' << Pspeed() << endl; } };
int main() { cout << '请按顺序输入一台组装机芯片(等级、频率、电压)、内存(品牌、主频、容量)、光驱参数(品牌、传输速率):' << endl; int newrank, newfrequency, newvoltage; cin >> newrank >> newfrequency >> newvoltage;
string name;
int RAM_frequency, RAM_capacity;
cin >> name >> RAM_frequency >> RAM_capacity;
string Cname;
int Cspeed;
cin >> Cname >> Cspeed;
Computer A(newrank, newfrequency, newvoltage, &name, RAM_frequency, RAM_capacity, &Cname, Cspeed);
A.run();
A.output1();
A.stop();
cout << '请按顺序输入一台品牌机内存大小、CPU的主频、光驱品牌:' << endl;
int ram_capacity, cpu_frequency;
string cname2;
cin >> ram_capacity >> cpu_frequency >> cname2;
Computer B(ram_capacity, cpu_frequency, &cname2);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nnlb 著作权归作者所有。请勿转载和采集!