定义学生类,并在主函数定义类的对象进行调用测试。
要求: ① 数据成员
② 具有无参和有参的构造函数。
③ 具有深拷贝构造函数。
④ 具有析构函数。
⑤ 具有输出函数、排序函数等。
C++类与对象 怎样把众多函数组合在一起?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- Italink 2018-11-13 14:09关注
#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; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥20 删除和修改功能无法调用
- ¥15 kafka topic 所有分副本数修改
- ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
- ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
- ¥40 串口调试助手打开串口后,keil5的代码就停止了
- ¥15 电脑最近经常蓝屏,求大家看看哪的问题
- ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档
- ¥50 C++五子棋AI程序编写
- ¥30 求安卓设备利用一个typeC接口,同时实现向pc一边投屏一边上传数据的解决方案。
- ¥15 SQL Server analysis services 服务安装失败