/*
*copyright(c) 2016,烟台大学计算机学院
*All rights reserved
*文件名称:test.cpp
*作者:李硕
*版本:Code::Blocks
*
*问题描述:宣告主权
*输入描述:
*程序输出:
*/
#include <iostream>
using namespace std;
class Student
{
public:
Student(int n,double s):num(n),score(s){}
void show();
int getScore(){return score;}
int getName(){return num;}
private:
int num; //学号
double score; //成绩
};
void Student::show()
{
cout<<num<<" "<<score<<endl;
}
//max函数返回arr指向的对象数组中的最高成绩者的学号(max并不是成员函数,而是普通函数)
int max(Student *arr);
int main()
{
int i;
Student stud[5]=
{
Student(101,78.5),Student(102,85.5),Student(103,100),
Student(104,98.5),Student(105,95.5)
};
Student *stu;
stu=stud;
for(i=0;i<5;i+=2)
{
cout<<"student"<<i+1<<":";
stu[i].show();
}
//输出第1、3、5个学生的信息(用循环语句)
//输出成绩最高者的学号
cout<<"5个学生中成绩最高者的学号为: "<<max(stu);//调用函数显示最高成绩
return 0;
}
int max(Student *arr)
{
double Max=0;
int i,j=0;
for(i=0;i<5;i++)
{
if(arr[i].getScore()>Max)
{
Max=arr[i].getScore();
j=i;
}
}
return arr[j].getName();
}
<img src="https://img-blog.csdn.net/20160428194136142?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />