C++ 程序错误分析:计算机类构造函数、RAM 和 CDROM 类初始化问题
以下是一个 C++ 程序,请告诉我错误在哪里?
#pragma once
#include<iostream>
#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()
{
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 = NULL;
RAM_capacity = NULL;
}
~RAM()
{
cout << 'Good bye!' << endl;
}
};
class CDROM {
private:
string Cname;
int speed;
public:
CDROM(string CName, int Cspeed)
{
Cname = CName;
speed = Cspeed;
};
CDROM()
{
Cname = '未知';
speed = NULL;
};
};
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) {};
void run() { cout << 'Computer开始运行!' << endl; }
void stop() { cout << 'Computer停止运行!' << endl; };
void output1()
{
cout << '我是组装机' << endl;
cout << 'CPU:' << endl;
cout << '等级:' << outRank() << '\n' << '频率:' << outF() << '\n' << '电压:' << outV() << 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 (int newrank, int newfrequency, int newvoltage, string Name, int RAM_frequency, int RAM_capacity, string CName, int Cspeed);
cout << '等级:' << A.outRank() << endl;
return 0;
}
以下是错误:
- 在 Computer 类的构造函数中,应该使用参数而不是变量类型:
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 类的构造函数调用中,不应该使用变量类型:
Computer A(int newrank, int newfrequency, int newvoltage, string Name, int RAM_frequency, int RAM_capacity, string CName, int Cspeed);
应该改为:
Computer A(newrank, newfrequency, newvoltage, &Name, RAM_frequency, RAM_capacity, &CName, Cspeed);
- RAM 类的构造函数中,应该使用 nullptr 而不是 NULL:
RAM()
{
name = '未知';
RAM_frequency = nullptr;
RAM_capacity = nullptr;
}
- CDROM 类的构造函数中,应该使用 0 而不是 NULL:
CDROM()
{
Cname = '未知';
speed = 0;
}
原文地址: https://www.cveoy.top/t/topic/nncM 著作权归作者所有。请勿转载和采集!