C++类中的构造函数内的数组应该如何初始化??

mulang023 2011-03-21 10:12:08
这个程序是我们学校C++实验的内容:
1. 某大学开田径运动会,现有12名选手参加100米比赛,对应的运动员号及成绩如表所示,请按运动员号顺序输入数据,按照成绩排名并输出,每一行输出一名运动员的名次、运动员号及成绩。要求用冒泡法排序。

运动员号 成绩(秒) 运动员号 成绩(秒)
001 13.6 031 14.9
002 14.8 036 12.6
010 12.0 037 13.4
011 12.7 102 12.5
023 15.6 325 15.3
025 13.4 438 12.7


弄了好久,但是在构造函数内Ath的数组num[3]应该如何初始化不知道怎么弄??


#include <iostream>
using namespace std;

//---------------------------------------------------
class Ath
{
private:
int num[3];
float score;
int rank;
public:
Ath(int n[3]={0,0,0},float s=0,int r=1); //构造函数中的数组如何初始化??
void input();
void display();
};

int main()
{int i,j;
Ath temp ;
Ath a[12]={Ath({0,0,1}),Ath({0,0,2}),Ath({0,1,0}),\
Ath({0,1,1}),Ath({0,2,3}),Ath

({0,2,5}),\
Ath({0,3,1}),Ath({0,3,6}),Ath

({0,3,7}),\
Ath({1,0,2}),Ath({3,2,5}),Ath

({4,3,8}) };
for (i=0;i<12;i++)
a[i].input();
for (i=0;i<11;i++)
for (j=i+1;j<12;j++)
{if (a[i].score>a[j].score)
{temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
for (i=0;i<12;i++)
a[i].rank=i+1;
for(i=0;i<12;i++)
a[i].display();
return 0;
}
//--------------------------------------------------------

-----------
void Ath::input()
{
cout<<"Please input score:";
cin>>score;

}

void Ath::diplay()
{
cout<<rank<<" "<<num[0]<<num[1]

<<num[2]<<" "<<score<<endl;
}

Ath::Ath(int n[3],float s,int r)
{ num[0]=n[0];
num[1]=n[1];
num[2]=n[2];
score=s;
rank=r;
}
...全文
4208 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bucengyaoyuan 2013-11-23
  • 打赏
  • 举报
回复
Ath():num({}),score(0),rank(1){}; 还有,构造函数类成员的初始化应该是在初始化列表中吧,在构造函数体内的应该叫做赋值,上面的初始化式我在g++上测试过,有warnning,不过可以初始化成功,num[]数组中的数会被默认初始化为0,当然,你也可以在num({})中的{}内分别初始化整个数组中的每个数,希望可以帮你解决问题。
feile922 2011-03-23
  • 打赏
  • 举报
回复
冒包了
fatshaw 2011-03-23
  • 打赏
  • 举报
回复
不用使用定长数据组作为成员变量,使用指针的方式会比较好点
碎碎念 2011-03-22
  • 打赏
  • 举报
回复
内容也不是很多,一个个赋值吧...
CyberLogix 2011-03-22
  • 打赏
  • 举报
回复
一个个赋值,或者使用int n[3]={0,0,0}初始化
cswuyg 2011-03-22
  • 打赏
  • 举报
回复
1、建议楼主使用string保存运动员号,或者使用int保存运动员号,而不是使用int数组!!
2、至于数组的初始化,那就是直接赋值咯,给你写个例子:
//2011.3.22
//Code::Blocks vs2010
#include <iostream>
using namespace std;
/////////////////////////////////////////////////////////
class Ctemp
{
public:
Ctemp(int (*piarr)[3], double dtemp) : dscore(dtemp)
{
memcpy(inum, piarr, sizeof(int)*3);
}
void display()
{
for(int i = 0; i != 3; ++i)
{
cout << inum[i];
}
cout << " " << dscore << endl;
}
private:
int inum[3];
double dscore;
};
/////////////////////////////////////////////////////////
int main()
{
int a[3] = {1,2,3};
int b[3] = {4,5,6};
int c[3] = {7,8,9};
Ctemp cta[3] = {Ctemp(&a, 1.1),Ctemp(&b, 1.2),Ctemp(&c, 1.3)};
for(int i = 0; i != 3; ++i)
{
cta[i].display();
}
system("pause");
return 0;
}
/*
123 1.1
456 1.2
789 1.3
请按任意键继续. . .
*/
mskmc_mc 2011-03-22
  • 打赏
  • 举报
回复
我滴神啊,如果这是教课书上的代码,那么崩溃吧
如果是楼主自己写的,那我无话可说。。。。
harleypang 2011-03-22
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 fendouzhe 的回复:]
Ath(int n[3]={0,0,0},float s=0,int r=1);
数组参数的这种默认值的赋值方式还是第一次看到,可行么?
[/Quote]

============
貌似不行吧?
fendouzhe 2011-03-22
  • 打赏
  • 举报
回复
Ath(int n[3]={0,0,0},float s=0,int r=1);
数组参数的这种默认值的赋值方式还是第一次看到,可行么?
wx376752150 2011-03-22
  • 打赏
  • 举报
回复
class Ath
{
private:
int num[3];
float score;
int rank;
public:
Ath(int n[3]={0,0,0},float s=0,int r=1); //构造函数中的数组如何初始化??
void input();
void display();
};

你问号出的地方是不对的,不管什么函数声明,数组名编译器就认为是指针,所以,
Ath(int n[3],float s=0,int r=1);
Ath(int n[],float s=0,int r=1);
Ath(int ×n,float s=0,int r=1);
三者等同,[]中有没有3都一样,所以你这样给一个指针赋予默认值是不对的。
只能赋予默认NULL,
还有,虽然传递数组的时候不传数组大小也可以通过编译,但是在运行时候会产生运行时的错误,比如数组越界之类,所以还要传数组大小。
至于怎么初始化,可以在构造函数里赋值。(如果没有赋值,默认为0,,这个我不确定,你自己做个实验),使用的时候,给构造函数传来个别的数组名和数组大小即可。希望能帮到你。
waxilo1988 2011-03-22
  • 打赏
  • 举报
回复
你那个是 构造函数么 好奇怪啊!!!
A(int a,int b,int c,float s=0,int r=1)
{
num[0]=a;
num[1]=b;
num[2]=c;
score=s;
rank=r;
}
貌似这样的!!!!!
elegant87 2011-03-21
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 pengzhixi 的回复:]
一个个赋值吧
[/Quote]
数据很多的话
可以考虑从文件中读取
pengzhixi 2011-03-21
  • 打赏
  • 举报
回复
一个个赋值吧

64,318

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧