【东华大学oj】14 对象数组和对象指针(面向对象)

该篇文章介绍了如何在C++中,利用Student类创建对象数组,并通过max函数找出成绩最高者,使用指针作为参数传递数组元素进行比较。
摘要由CSDN通过智能技术生成

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;

    }

  • 7
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ixll625

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值