【4.17】
#include<iostream>
using namespacestd;
class student
{
public:
voidsetnum(int a,intb)
{
studentID= a;
score= b;
}
voiddisp()
{
cout<< " "<<studentID<< " "<< score << endl;
}
private:
intstudentID, score;
};
int main()
{
student num[6];
student *pr;
num[0].setnum(01,90);
num[1].setnum(02,85);
num[2].setnum(03,95);
num[3].setnum(04,85);
num[4].setnum(05,90);
num[5].setnum(06,80);
pr =num;
cout<< "学号" << " "<< "成绩" << endl;
pr =pr + 1;
pr->disp();
pr=pr+2;
pr->disp();
pr=pr+2;
pr->disp();
system("pause");
return0;
}
【4.18】
#include<iostream>
using namespacestd;
static inth = 0;
int m;
class student
{
public:
voidsetnum(int a,intb)
{
studentID= a;
score= b;
}
voidmax()
{
if(score > h)
h= score, m = studentID;
};
private:
intstudentID, score;
};
int main()
{
student num[6];
student *pr;
num[0].setnum(01,90);
num[1].setnum(02,85);
num[2].setnum(03,95);
num[3].setnum(04,87);
num[4].setnum(05,92);
num[5].setnum(06,80);
for(pr = num; pr<num + 6; pr++)
pr->max();
cout<< "分数最高的同学的学号是:" << endl;
cout<< m << endl;
system("pause");
return0;
}