帮忙改改C++小程序,怎样实现Computer的调用??
答案:1 悬赏:50 手机版
解决时间 2021-04-30 00:42
- 提问者网友:山高云阔
- 2021-04-29 18:48
#include
using namespace std;
class CPU{
private:
int cpu;
public:
CPU(int i){
cpu=i;
cout<<"the kind of cpu is:"<<cpu<<endl;
cout<<"构造了一个CPU"<<endl;}
~CPU(){cout<<"析构了一个CPU"<<endl;}
};
class RAM{
private:
int ram;
public:
RAM(int j){
ram=j;
cout<<"the kind of ram is:"<<ram<<endl;
cout<<"构造了一个RAM"<<endl;}
~RAM(){cout<<"析构了一个RAM"<<endl;}
};
class CDROM{
private:
int cdrom;
public:
CDROM(int k){
cdrom=k;
cout<<"the kind of cdrom is:"<<cdrom<<endl;
cout<<"构造了一个CDROM"<<endl; }
~CDROM(){cout<<"析构了一个CDROM"<<endl;}
};
class Computer{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
Computer(int i,int j,int k){
cpu=i;
ram=j;
cdrom=k;
CPU a(cpu);
RAM b(ram);
CDROM c(cdrom);
cout<<"构造了一个Computer"<<endl;}
~Computer(){cout<<"析构了一个Computer"<<endl;}
void run(){cout<<"kaishi"<<endl;}
void stop(){cout<<"tingzhi"<<endl;}
};
void main(){
Computer d(1,2,3);
d.run();
d.stop();
}
最佳答案
- 五星知识达人网友:酒醒三更
- 2021-04-29 19:09
错误主要在于 Computer 类的构造函数。cpu=i;ram=j;cdrom=k;这三句出现错误,i,j,k是基本数据类型 int,而cpu,ram,cdrom是自定义类的对象,直接赋值造成了类型不匹配。
C++中规定:如果一个类 A 中含有另一个类 B 的对象 b,作为其数据成员。那么该对象 b 的初始化,应该在 A 的构造函数基础上实现。实现如下:
A():b(){}.该题目中 Computer 类的实现,就应该如下:
Computer(int i,int j,int k):cpu(i),ram(j),cdrom(k)
{
cout<<"构造了一个Computer"<<endl;
}
整体程序修改如下:
#include <iostream>
using namespace std;
class CPU
{
private:
int cpu;
public:
CPU(int i)
{
cpu=i;
cout<<"the kind of cpu is:"<<cpu<<endl;
cout<<"构造了一个CPU"<<endl;
}
~CPU(){cout<<"析构了一个CPU"<<endl;}
};
class RAM
{
private:
int ram;
public:
RAM(int j)
{
ram=j;
cout<<"the kind of ram is:"<<ram<<endl;
cout<<"构造了一个RAM"<<endl;
}
~RAM(){cout<<"析构了一个RAM"<<endl;}
};
class CDROM
{
private:
int cdrom;
public:
CDROM(int k)
{
cdrom=k;
cout<<"the kind of cdrom is:"<<cdrom<<endl;
cout<<"构造了一个CDROM"<<endl;
}
~CDROM(){cout<<"析构了一个CDROM"<<endl;}
};
class Computer
{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
Computer(int i,int j,int k):cpu(i),ram(j),cdrom(k)
{
//cpu=i;
//ram=j;
//cdrom=k;
//CPU a(cpu);
//RAM b(ram);
//CDROM c(cdrom);
cout<<"构造了一个Computer"<<endl;
}
~Computer(){cout<<"析构了一个Computer"<<endl;}
void run(){cout<<"kaishi"<<endl;}
void stop(){cout<<"tingzhi"<<endl;}
};
void main()
{
Computer d(1,2,3);
d.run();
d.stop();
}
C++中规定:如果一个类 A 中含有另一个类 B 的对象 b,作为其数据成员。那么该对象 b 的初始化,应该在 A 的构造函数基础上实现。实现如下:
A():b(){}.该题目中 Computer 类的实现,就应该如下:
Computer(int i,int j,int k):cpu(i),ram(j),cdrom(k)
{
cout<<"构造了一个Computer"<<endl;
}
整体程序修改如下:
#include <iostream>
using namespace std;
class CPU
{
private:
int cpu;
public:
CPU(int i)
{
cpu=i;
cout<<"the kind of cpu is:"<<cpu<<endl;
cout<<"构造了一个CPU"<<endl;
}
~CPU(){cout<<"析构了一个CPU"<<endl;}
};
class RAM
{
private:
int ram;
public:
RAM(int j)
{
ram=j;
cout<<"the kind of ram is:"<<ram<<endl;
cout<<"构造了一个RAM"<<endl;
}
~RAM(){cout<<"析构了一个RAM"<<endl;}
};
class CDROM
{
private:
int cdrom;
public:
CDROM(int k)
{
cdrom=k;
cout<<"the kind of cdrom is:"<<cdrom<<endl;
cout<<"构造了一个CDROM"<<endl;
}
~CDROM(){cout<<"析构了一个CDROM"<<endl;}
};
class Computer
{
private:
CPU cpu;
RAM ram;
CDROM cdrom;
public:
Computer(int i,int j,int k):cpu(i),ram(j),cdrom(k)
{
//cpu=i;
//ram=j;
//cdrom=k;
//CPU a(cpu);
//RAM b(ram);
//CDROM c(cdrom);
cout<<"构造了一个Computer"<<endl;
}
~Computer(){cout<<"析构了一个Computer"<<endl;}
void run(){cout<<"kaishi"<<endl;}
void stop(){cout<<"tingzhi"<<endl;}
};
void main()
{
Computer d(1,2,3);
d.run();
d.stop();
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯