用c++实现创建学生类,并依据学生成绩来排序

1.题目:

创建一个对象数组,数组的元素是学生对象,学生的信息包括学号、姓名和成绩(一门课成绩),在main函数中将数组元素按学生成绩从小到大的顺序排序并显示出来。(学生对象不得少于5个,显示定义构造函数和析构函数,并在里面加上测试语句)

2.代码:

#include<iostream>
#include<string>
using namespace std;
class Student
{
public:
  Student()
  {
      cout << "已经调用了无参函数!!!!!!!!"<<endl;
  }
  Student(string Name,string Num,int Score)
  {
      name = Name;
      num  = Num;
      score = Score;
      cout << "已经调用了含参构造函数!!!!" <<endl;
  }
  ~Student()
  {
      cout <<"已经调用了析构函数!!!"<<endl;
  }
  void stu_show()
  {
      cout << "学生的学号为:" << num << ",学号为:"  << name <<",成绩: " << score << endl;
  }
  int  get_score()
  {
      return score;
  }
  int  set_score()
  {
      cin >> score;
  }
private:
  string name;
  string num;
  int score;
};

int main()
{
    Student stu[5] = {
    Student ("2111030616","王五",100),
    Student ("2111030615","张三",99),
    Student ("2111030614","李四",60),
    Student ("2111030622","黄六",96),
    Student ("2111030658","胡七",50),
    };
    for (int i = 0; i < 4 ;++i )
        {
            for (int j = 0 ; j < 4 - i  ; ++j)
            {
                if(stu[j].get_score()> stu [j+1].get_score())
              {
               Student tmp;
               tmp = stu[j];
               stu[j] = stu[j+1];
               stu[j+1] = tmp;
              }
            }
        }

  for (int i = 0; i < 5; ++i )
  {
      stu[i].stu_show();
  }
    return 0;
}

3.结果:

 


版权声明:本文为m0_64071735原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。