// 测试代码 int main() { string name1 = ' Kingston '; // RAM品牌 string name2 = '华硕'; // CDROM品牌 int a = 2400, b = 8; // RAM频率和容量 int c = 220, d = 3000; // CPU电压和频率 int e = 1600; // CDROM传输速率 Computer computer1(c, d, e, &name1, a, b, &name2, e); // 创建Computer对象并初始化 computer1.output1(); // 输出初始化的Computer对象信息 cout << endl;

Computer computer2(2400, 8, &name1); // 创建Computer对象并初始化
computer2.output1(); // 输出初始化的Computer对象信息
cout << endl;

return 0;

}

#pragma once #include #include<string.h> using namespace std;

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()//构造函数重载 { rank = P6; CPU_frequency = 3000; CPU_voltage = 220; } CPU(int b) { CPU_frequency = b; } ~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(string Name, int a, int b) { name = Name; RAM_frequency = a; RAM_capacity = b; }; RAM() { name = "未知"; RAM_frequency = 0; RAM_capacity = 0; } 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(string CName, int Cspeed) { Cname = CName; speed = Cspeed; }; CDROM() { Cname = "未知"; speed = 0; }; 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 a, int b, int c, string* N, int d, int e, string* M, int f) :CPU(a, b, c), RAM(*N, d, e), CDROM(M, f) {}; Computer(int b, int a, string N) :RAM(a), CPU(b), CDROM(*N) {}; void run() { cout << "Computer开始运行!" << endl; } void stop() { cout << "Computer停止运行!" << endl; }; void output1() {
cout << "我是组装机" << endl; cout << "CPU:" << endl; cout << "等级:" << outRank() << '\n' << "频率:" << outF() << '\n' << "电压:" << outV() << endl; cout << "RAM:" << endl; cout << "品牌:" << Pname() << '\n' << "频率:" << P_ram_frequency() << '\n' << "容量:" << P_ram_capacity() << endl; cout << "CDROM:" << endl; cout << "品牌:" << P_Cname() << '\n' << "传输速率:" << Pspeed() << endl; }; };

C++ #pragma once 指令详解及应用示例:自定义计算机类

原文地址: https://www.cveoy.top/t/topic/nnjN 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录