面向对象程序设计上机练习九(对象指针)

题目描述

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

输入

输入5个学生数据。

输出

输出5个学生中成绩最高者的学号和成绩。

示例输入

01 89
02 78
03 56
04 92
05 76

示例输出

04 92
#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
 string id;
 int score;
 void set()
 {
     cin>>id>>score;
 }
};
Student students[5];
void max(Student* &s)
{
    int m=students[0].score;
    int i,k=0;
    for(i=1;i<5;i++)
    {
        if(students[i].score > m )
        {
            m=students[i].score;
            s=&students[i];
        }
    }
}

int main()
{
 for(int i=0;i<5;i++)
 {
     students[i].set();
 }
 Student *p=&students[0];
 max(p);
 cout<<p->id<<" "<<p->score<<endl;
 return 0;
}
posted @ 2014-10-20 23:18  夏迩  阅读(231)  评论(0)    收藏  举报