C++类中的构造函数内的数组应该怎么初始化?
C++类中的构造函数内的数组应该如何初始化??
这个程序是我们学校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;
}
------解决方案--------------------
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,,这个我不确定,你自己做个实验),使用的时候,给构造函数传来个别的数组名和数组大小即可。希望能帮到你。
------解决方案--------------------
1、建议楼主使用string保存运动员号,或者使用int保存运动员号,而不是使用int数组!!
2、至于数组的初始化,那就是直接赋值咯,给你写个例子:
这个程序是我们学校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;
}
------解决方案--------------------
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,,这个我不确定,你自己做个实验),使用的时候,给构造函数传来个别的数组名和数组大小即可。希望能帮到你。
------解决方案--------------------
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
请按任意键继续. . .