求解C++对象数组的一道题!!!

来源:百度知道 编辑:UC知道 时间:2024/05/14 12:26:04
建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生成绩最高者,并输出学号!用类做.本人刚学c++,很多 不懂,希望各位能帮个忙!

#include <iostream>
using namespace std;

class Student
{
public:
Student(int n,float s):number(n),score(s) {} //声明构造函数 定义构造函数

int number;
float score;
};

void max( Student *y) // "函数max,用指向对象的指针作函数参数"这样才对吧,不然怎么做5个成绩的比较呢
{
float t;
int k;
for(int i=0;i<4;i++)
for(int j=0;j<4-i;j++)
if((y+i)->score>(y+i+1)->score) //把所有数据都来交换,目的是让stud[4]存放最大分数
{
t=(y+i)->score; //交换分数
(y+i)->score=(y+i+1)->score;
(y+i+1)->score=t;

k=(y+i)->number; //交换学号
(y+i)->number=(y+i+1)->number;
(y+i+1)->number=k;
}

cout<<"成绩最高者 "<<(y+4)->score <<" 学号 "<<(y+4)->number <<endl;

}
int main()
{
Student stud[5]={
Student(1001,75),
Student(1002,