【问题描述】
建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号和成绩。
【样例输入】
无。
【样例输出】
104 100
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Student
{
public:
Student(int n , float s ) :id(n), score(s) {};//构造函数
void display();
int id;
float score;
};
void Student::display()
{
cout << id << " " << score<< endl;
}
void max(Student *p)
{
int i, max_i = 0;
float max_s=0;
for (i = 0;i<5;i++)
if ((p + i)->score>max_s)
{
max_s = (p + i)->score;
max_i = i;
}
(p + max_i)->display();
}
int main()
{
Student stud[5] = {
Student(101,78.5),Student(102,85.5),Student(103,98.5),
Student(104,100.0),Student(105,95.5) };
Student *p = &stud[0];
max(p);
return 0;
}