首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

对象的指针做函数参数的调用~初学者`

2012-03-22 
对象的指针做函数参数的调用~~~~菜鸟求救```//输入五个学生的分数,然后比较分数的大小,然后输出最高分的那

对象的指针做函数参数的调用~~~~菜鸟求救```
//输入五个学生的分数,然后比较分数的大小,然后输出最高分的那个人的学号和分数,


#include <iostream>
using   namespace   std;
class   Student
{      
public:
  Student(int   n=0,int   s=0):num(n),score(s){}
          void   set_date();
          void   show_date();
         
//private:
  int   num;
  int   score;
};   Student   stu[5];
void   Student::set_date()
{  
          cout < < "please   enter   the   date   of   student " < < ": " < <endl;
          cin> > num;
  cin> > score;
}
void   display(void)
{      
int   i=0;
for   (i;i <5;i++)   {   stu[i].set_date();
}


void   max(Student   *arr)//就是这里我搞错了拉~~~我把前面的数据成员定义为
{                                           //public时也错了,大家告诉我错在哪了咯,为什么错详细
      int   i=0,k=0;               //点哈,本人菜鸟```想搞明白点,还有就是我要是就是把数
      int   max=arr[0].score;//成员定义为private那又怎么办乐,就2个问题,希望
      for   (i;i <5;i++)             //大家帮下小弟````
      {
      if(max <arr[i].score)
      {max=arr.score[i];k=i}
      }
      cout < <arr[k].num < < "   " < <max < <endl;
}

}
int   main()
{       display();
        Student   *p;
*p=&stu[0];
max(p);
        return   0;

}

[解决办法]

#include <iostream>
using namespace std;
class Student
{
public:
Student(int n=0,int s=0):num(n),score(s){}
void set_date();
void show_date();
//void max(Student *arr);

//private:
public:
int num;
int score;
}; Student stu[5];
void Student::set_date()
{
cout < < "please enter the date of student " < < ": " < <endl;
cin> > num;
cin> > score;
}
void display(void)
{
int i=0;
for (i;i <5;i++) stu[i].set_date();//去掉{括号
}


void max(Student *arr)//就是这里我搞错了拉~~~我把前面的数据成员定义为
{ //public时也错了,大家告诉我错在哪了咯,为什么错详细
int i=0,k=0; //点哈,本人菜鸟```想搞明白点,还有就是我要是就是把数
int max=arr[0].score;//成员定义为private那又怎么办乐,就2个问题,希望
for (i;i <5;i++) //大家帮下小弟````
{
if(max <arr[i].score)
{
max=arr[i].score;//这里改
k=i;
}//少分号
}
cout < <arr[k].num < < " " < <max < <endl;
}

//}
int main(){
display();
Student *p;
p=&stu[0];//去掉*
max(p);
return 0;
}


错误一大堆,楼主要仔细点啊
[解决办法]
//下面的代码是完全正确的。
//可以实现你要的功能。
#include <iostream>
using namespace std;
class Student
{
friend void max(Student *arr); //将max声明为类的友元函数,就可以解决问题了
public:


Student(int n=0,int s=0):num(n),score(s){}
void set_date();
void show_date();
private:
int num;
int score;
};Student stu[5];

void Student::set_date()
{
cout < < "please enter the date of student " < < ": " < <endl;
cin> > num;
cin> > score;
}
void display(void)
{
int i=0;
for (i;i <5;i++)
{
stu[i].set_date();
}
}


void max(Student *arr) //将max声明为Student类的友元函数,这样就可以访问Student类的
{ //私有成员了
int i=0,k=0;
int max=arr[0].score;
for (i=0;i <5;i++)
{
if(max <arr[i].score)
{
max=arr[i].score;
k=i;
}
}
cout < <arr[k].num < < " " < <max < <endl;
}

int main()
{
display();
Student *p;
p=&stu[0];
max(p);
return 0;
}

热点排行