注册 登录
编程论坛 C++教室

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

Kevin666 发布于 2017-12-21 17:20, 2693 次点击
#include <iostream>
//6. 建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用指向对象的指针作函数参数,
//在max函数中找出5个学生中成绩最高者,并输出其学号。初值自拟。
using namespace std;
class student
{
public:
    student(int s,int n)
    {
        score=s;
        number=n;

    }
    void display();
    friend int max1(student*p);
private:
    int score;
    int number;
};
void student::display()
{
    cout<<score<<number;
}
int max1(student*p)
{
    int flag;
    int max1=p->score;
    for(int i=0;i<5;i++)
    {
        if(max1<(p+i)->score)
        {
            max1=(p+i)->score;
            flag=i;

        }

    cout<<max1;
    return (p+flag)->number;
}
int main()
{
    student ad[5]={
    student(18,1),
    student(16,2),
    student(19,3),
    student(15,4),
    student(11,5)
    };
    cout<<max1(ad)<<endl;
    return 0;
}
1 回复
#2
Kevin6662017-12-21 17:30
找到了 各位不用看了
缺少一个}
1