向对象程序设计上机练习九(对象指针)
Time Limit: 1000MS Memory limit: 65536K
题目描述
建立对象数组,内放5个学生数据(学号是字符串类型、成绩是整型),设立max函数,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号和成绩。
输入
输入5个学生数据。
输出
输出5个学生中成绩最高者的学号和成绩。
示例输入
01 89 02 78 03 56 04 92 05 76
示例输出
04 92
#include <iostream>
#include <algorithm>
#include <cstdio>
using namespace std;class dd
{
public:string name;int score;void gete(){cin>>name>>score;}}s[5];dd max(dd *p, dd *q )
{dd *w;if(q->score > p->score ){w = p; p = q; q = w;}return *p;
}int main()
{int i;for(i=0; i<5; i++){s[i].gete();}dd *t;t=&s[0];for(i=1; i<5; i++){dd *bomb;bomb = &s[i];*t = max(t, bomb);}cout<<t->name<<' '<<t->score<<endl;return 0;
}