建立一个对象数组,内放5个学生的(学号,成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生的最高成绩者,并输出其学号。

#include<iostream>
using namespace std; class Student
{public:
Student(int=,int=);
int number;
int score;
void display();
}; Student::Student(int num,int sco):number(num),score(sco){
} void Student::display()
{
cout<<number<<" "<<score<<endl;
} void max(Student *p)
{
int maxi=p[].score;
int temp=;
for(int i=;i<;i++)
if(p[i].score>maxi)
{
maxi=p[i].score;
temp=i;
}
cout<<p[temp].number<<" "<<maxi<<endl;
} int main()
{
Student stu[]={
Student{,},
Student{,},
Student{,},
Student{,},
Student{,}
};
Student *p=&stu[];
max(p);
return ;
}C++之类和对象课后习题1-LMLPHP
05-11 13:36