定义学生类,并在主函数定义类的对象进行调用测试。
要求: ① 数据成员
② 具有无参和有参的构造函数。
③ 具有深拷贝构造函数。
④ 具有析构函数。
⑤ 具有输出函数、排序函数等。
#include<iostream>
#include<string>
using namespace std;
class Student{
public:
int NO=0;
string name="";
int score[3] = { 0 };
double average;
Student(){} //无参构造
Student(int,string,int,int,int ); //构造
Student(Student &); //拷贝构造
void show();
~Student() {} //析构
};
Student::Student(int id,string name,int score1,int score2,int score3){
this->NO = id;
this->name = name;
this->score[0] = score1;
this->score[1] = score2;
this->score[2] = score3;
this->average = double(score1 + score2 + score3) / 3;
}
Student::Student(Student &other) {
this->NO = other.NO;
this->name = other.name;
for (int i = 0; i < 3; i++)
this->score[i] = other.score[i];
}
void Student::show() {
if (NO != 0)
cout << NO << "\t" << name << "\t" << score[0] << "\t" << score[1] << "\t" << score[2] <<"\t"<<average<< endl;
else
cout << "No\tname\tscore1\tscore2\tscore3\taverage" << endl;
}
void sort(Student t[],int len) {
Student tmp;
for (int i = 0; i < len - 1; i++) {
for (int j = i + 1; j < len; j++) {
if (t[i].average < t[j].average) {
tmp = t[i];
t[i] = t[j];
t[j] = tmp;
}
}
}
}
int main() {
Student Test[6] = { {},
{101,"Zhou",93,89,87},
{102,"Yang",85,80,78},
{103,"Chen",77,70,83},
{104,"Qian",70,67,60},
{105,"Li",72,70,69}};
for (int i = 0; i < 6; i++)
Test[i].show();
sort(Test, 6);
cout << " 排序后:" << endl;
for (int i = 0; i < 6; i++)
Test[i].show();
return 0;
}