/*【任务4】设计一个学生类,包括学号(num)和成绩(score)。建立一个对象数组,内放5个学生的数据,要求:
(1) 用指针指向数组首元素,输出第1、3、5个学生的信息;
(2) 设计一个函数max,用指向对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。*/
#include<iostream>
using namespace std;
class student
{
public:
student(int n = 0, float sc = 60) : num(n), score(sc){};
void max(student *arr);
void display();
private:
int num;
int score;
};
void student :: display()
{
cout << num << " " << score << endl;
}
void student :: max(student *arr)
{
float max_score = arr[0].score;
int k;
for(int j = 1; j < 5; ++j)
{
if(arr[j].score > max_score)
{
max_score = arr[j].score;
k = j;
}
}
cout << "最高成绩同学的学号和成绩:" << endl << endl;
cout << arr[k].num << " " << max_score << endl;
}
int main()
{
student stud1;
student stud[5] = {
student(11, 80),
student(12, 79),
student(13, 89),
student(14, 96),
student(15, 81)
};
student *p = stud;
for(int i = 0; i <= 2; p += 2, ++i)
{
p->display();
}
cout << endl;
p->max(stud);
return 0;
}

还没有评论,来说两句吧...