C++指针作参数问题,高手帮下忙。

来源:百度知道 编辑:UC知道 时间:2024/04/28 21:08:43
建立一个对象数组,内放5个学生的数据(学号,成绩),建立一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。

#include"iostream"
using namespace std;
class Student
{
public:
Student(int=1001,float=80); //声明构造函数
float max(float *y); //声明成员函数
private:
int number;
float score;
};

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

float Student::max(float *y)
{
*y=score;
return score;
}

int main()
{
Student stud[5]={
Student(1001,86.5),
Student(1002,89),
Student(1003,98),
Student(1004,65.5),
Student(1005,95)};
stud[1].max();
return 0;

}

小弟我是初学者,只能写到这样了,程序还不完整,哪位大哥帮帮忙,小弟感激不尽。

#include"iostream"
using namespace std;
class Student
{
public:
Student(int=1001,float=80); //声明构造函数

int number;
float score;
};

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

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 <<e