#include<iostream>
using namespace std;
class Student
{
private:
int num;
double score;
public:
Student(int a,double b):num(a),score(b) {};
void set_students();
void get_students();
int return_num();
int return_score();
void get_max();
};
void Student::set_students()
{
cout<<"输入学生的学号";
cin>>num;
cout<<"输入学生的成绩";
cin>>score;
}
void Student::get_students()
{
cout<<num<<"学生的成绩为"<<score<<endl;
}
int Student::return_num()
{
return num;
}
int Student::return_score()
{
return score;
}
int max(Student*arr);
int main()
{
Student stud[5]= {Student(101,78.5),Student(102,85.5),Student(103,100),Student(104,98.5),Student(105,95.5)};
Student*p;
p=stud;
int i;
for(i=0; i<5; i+=2)
{
p[i].get_students();
}
cout<<"五个学生中成绩最高者的学号为"<<max(stud)<<endl;
return 0;
}
int max(Student*arr)
{
int i;
int a=0;
int save;
double b=arr[0].return_score();
for(i=0; i<4; i++)
{
if(b<arr[i].return_score())
{
a=i;
b=arr[i].return_score();
}
}
save=arr[a].return_num();
return a=save;
}
又把指指针和数组复习了一遍,加深了对数组和指针的认识。