14 对象数组和对象指针
作者: 冯向阳时间限制: 1S章节: 类与对象
问题描述 :
要求:在给定的Student类的基础上建立一个对象数组,内放n个学生的数据(学号,分数)。请设计一个用户函数max,用指向对象的指针作为函数参数。max函数的功能是找出这n个学生中成绩最高者(假定无相同的成绩),并输出其学号。
Student类如下:
class Student{
public:
void Set_StuNum(int); //自行设计
int Get_StudNum(); //自行设计
void Set_Score(float); //自行设计
float Get_Score(); //自行设计
private:
int stu_num; //学号
float score; //分数
};
max函数原型为:
void max(Student *, int, int &, float &); //5个形参的含义分别为:对象指针,学生数,最高分的学生学号,最高分
要求使用以下main函数测试:
int main(){
int n, stu_num, max_stu_num;
float score, max_score;
cin>>n;
Student stu[n];
for( int i = 0; i < n; i++ ){
cin>>stu_num>>score;
stu[i].Set_StuNum(stu_num);
stu[i].Set_Score(score);
}
max(stu, n, max_stu_num, max_score);
cout<<max_stu_num<<" "<<max_score;
return 1;
}
输入说明 :
第一行:学生数目n。
第二行至第n+1行:每行输入:学号 分数。 二者之间以空格分隔,行与行之间无空行。
输出说明 :
最高分的学生学号 分数。二者之间以空格分隔
#include<iostream>
#include <cstring>
#include <cmath>
using namespace std;
class Student
{
private:
int stu_num; //学号
float score; //分数
public:
Student() {}
void Set_StuNum(int num)
{
stu_num=num;
}
int Get_StudNum()
{
return stu_num;
}
void Set_Score(float s)
{
score=s;
}
float Get_Score()
{
return score;
}
};
void max(Student *stu, int n, int &max_nu, float &max_stu)
{
max_stu=stu[0].Get_Score();
max_nu=stu[0].Get_StudNum();
for(int i=1; i<n; i++)
{
if(stu[i].Get_Score()>max_stu)
{
max_stu= stu[i].Get_Score();
max_nu= stu[i].Get_StudNum();
}
}
}
int main()
{
int n, stu_num, max_stu_num;
float score, max_score;
cin>>n;
Student stu[n];
for( int i = 0; i < n; i++ )
{
cin>>stu_num>>score;
stu[i].Set_StuNum(stu_num);
stu[i].Set_Score(score);
}
max(stu, n, max_stu_num, max_score);
cout<<max_stu_num<<" "<<max_score;
return 0;
}