wl991001 2018-11-13 12:53 采纳率: 0%
浏览 525
已采纳

C++类与对象 怎样把众多函数组合在一起?

定义学生类,并在主函数定义类的对象进行调用测试。
要求: ① 数据成员
② 具有无参和有参的构造函数。
③ 具有深拷贝构造函数。
④ 具有析构函数。
⑤ 具有输出函数、排序函数等。

  • 写回答

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;
    }
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题