c++程序问题 。建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个 20
#include<iostream>
#include<string.h>
using namespace std;
class student
{
public:
student(string number,int id)
{
this->number=number;
this->id=id;
}
int getscore(){
return id;
}
string getname(){
return number;
}
private:
string number;
int id;
};
int main()
{
student stu[5]={student("001",100),student("002",100),student("003",99),student("004",95),student("0055",86)};
int max_score=stu[0].getscore();
for(int i=0;i<5;i++)
{
if (max_score <stu[i].getscore())
{
max_score=stu[i].getscore();
}
}
cout<<"最高分为"<<max_score<<endl;
for(int j=0;j<5;j++)
{
if(stu[j].getscore()==max_score)
{
cout<<"最高分的学生学号为:"<<stu[j].getname()<<endl;
}
}
}
//运行结果为:
./a.out
最高分为100
最高分的学生学号为:001
最高分的学生学号为:002