C++多重继承构造函数错误及解决方法
以下是一个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(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;
};
};
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 a, b;
string Cname2;
cin >> a >> b >> Cname2;
Computer B(b,a,Cname2);
return 0;
}
错误在于构造函数的调用方式不正确,应该使用基类构造函数初始化列表来初始化基类成员,而不是在子类构造函数中直接调用基类构造函数。应该修改如下:
class Computer :public CPU,public RAM,public CDROM {
public:
Computer(int a, int b, int c, const string& N, int d, int e, const string& M, int f)
:CPU(a, b, c), RAM(N, d, e), CDROM(M, f) {};
Computer(int b, int a, const string& N)
:CPU(b), RAM(N, 0, a), CDROM() {};
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;
};
};
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 a, b;
string Cname2;
cin >> a >> b >> Cname2;
Computer B(b, a, Cname2);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/nniH 著作权归作者所有。请勿转载和采集!