1. 程式人生 > >c++:指向學生類的指標:求最高成績

c++:指向學生類的指標:求最高成績

#include <iostream>


using namespace std;


class Student
{
public:
    Student(int n,double s)
    {
        num=n;
        score=s;
    }
void out_put(Student stu[],int i)
{


    cout<<"第"<<i+1<<"個學生學號為:"<<stu[i].num<<"  學生成績為:"<<stu[i].score<<endl;


}
int getNumber()
{
    return num;
}
double getScore()
{
    return score;
}
private:
    int num;
    double score;
};
int Max(Student *A)
{
    int k;
    double MaxScore=A[0].getScore();
    for(int i=1;i<5;i++)
    {
        if(A[i].getScore()>MaxScore)                  
        {
            MaxScore=A[i].getScore();
            k=i;
        }
    }
    return A[k].getNumber();
}
int main()
{
    Student stu[5]=
    {
        Student(1001,86.5),Student(1002,85.5),Student(1003,100),
        Student(1004,98.5),Student(1005,95.5)
    };
    for(int i=0;i<5;i=i+2)
    stu[i].out_put(stu,i);
    cout<<"5個學生中成績最高的學號為:"<<Max(stu)<<endl;
    return 0;

}