2024年4月12日发(作者:诺基亚2652)
建立一个对象数组,内放5个学生的数据(学号、成绩),设立一个函数max,用 指向
对象的指针作函数参数,在max函数中找出5个学生中成绩最高者,并输出其学号。初值自
拟
#include
#include
class student
{
private:
std::string sno;
int score;
public:
/*student()
{
}*/
friend void max(student *p);
student(std::string a, int b)
{
sno = a;
score = b;
}
student(int b) :score(b)
{
}
void display()
{
std::cout << sno << " " << score << std::endl;
}
};
void max(student *p)
{
student *pp = NULL;
for (int i = 0; i < 4; i++)
{
if (p[i].score>p[i + 1].score)
{
pp = &p[i];
}
else
{
pp = &p[i + 1];
}
}
std::cout << pp->sno << " " << pp->score << std::endl;
}
void main()
{
student s[5] = {student("11", 2),student("12", 3),student("13", 4),student("14",
5),student("15", 6) };
student *p = s;
max(p);
system("pause");
}
发布者:admin,转转请注明出处:http://www.yc00.com/num/1712889002a2142221.html
评论列表(0条)