Magician_liu 2023-05-13 13:46 采纳率: 57.1%
浏览 19

class——student类

#include<iostream> 
//#include<time.h>
#include<string>
#include<vector>
using namespace std;

//class Bank
//{
//private:
//    long number;
//    double rest;
//    string records[50];
//    int N;
//public:
//    Bank(long number, double rest)
//    {
//        this->number = number;
//        this->rest = rest;
//        this->N = 0;
//    }
//    void Bankin(double m)
//    {
//        this->rest += m;
//        time_t now = time(0);
//        char x[30];
//        ctime_s(x, sizeof(x), &now);
//        this->records[N++] = (string)"交易时间为:" +x + (string)"交易类型为:存钱  " + (string)"交易金额为:" + to_string(m);
//    }
//    void Bankout(double m)
//    {
//        if (m > this->rest)
//        {
//            cout << "余额不足" << endl;
//            return;
//        }
//        this->rest -= m;
//        time_t now = time(0);
//        char x[30];
//        ctime_s(x, sizeof(x), &now);
//        this->records[N++] = (string)"交易时间为:" + x + (string)"交易类型为:取钱  " + (string)"交易金额为:" + to_string(m);
//    }
//    friend void disp(Bank& b);
//};
//void disp(Bank& b)
//{
//    cout <<b.number<<"账户剩余余额为:" << b.rest << endl;
//    cout << "该账户共有" << b.N << "条交易记录" << endl;
//    for (int i = 0; i < b.N; i++)
//    {
//        cout << b.records[i] << endl;
//    }
//}
const int x = 3;//课程数量
const int m = 2;//学生人数
class Student
{
private:
    string name;
    long long id;
    vector<double> price;
    static int sum, unpass;
public:
    Student() {
        this->id = 0;
        this->name = '0';
        price.push_back(0);
        price.push_back(0);
        price.push_back(0);
        
    }
    Student(string name, long long id, double cxsj, double xhcl, double sjjg)
    {
        this->id = id;
        this->name = name;
        price.push_back(cxsj);
        price.push_back(xhcl);
        price.push_back(sjjg);
        sum++;
        if (this->aver() < 60) unpass++;
    }
    double aver()
    {
        double sum = 0;
        for (int i = 0; i < x; i++)
        {
            sum += price[i];
        }
        return sum / x;
        
    }
    static  void disp();
    void print_price() {
        cout << "程序设计成绩为:" << this->price[0];
        cout << " 信号处理成绩为:" << this->price[1];
        cout << " 数据结构成绩为:" << this->price[2];
        cout << " 平均成绩为:" << this->aver() << endl;
    }
};
int Student::sum = 0;
int Student::unpass = 0;
void Student::disp()
{

    cout << "总人数为: " << sum << endl;
    cout << "不及格人数为:" << unpass << endl;
}
int main() {
    
    string name;
    long long id;
    double cxsj;
    double xhcl;
    double sjjg;

    Student a[m];
    for (int i = 0; i < m; i++)
    {
        cin >> name >> id >> cxsj >> xhcl >> sjjg;
        a[i]=(Student(name, id, cxsj, xhcl, sjjg));
        a[i].print_price();
    }
    Student::disp();
    
    return 0;
}

默认函数构造的重要性:
当我定义Student a[m]时,默认构造函数会被调用,如果默认构造函数被有参构造函数覆盖时,又不传参,会出现报错,此时应该重载无参构造函数
Student类实验中还遇到了前向引用申明的问题,尽管我声明了class类,但依旧不能在class细节实现前不能定义该类对象。还有本程序也有一定弊端,就是如果我想添加课程数量,那么也就得修改构造函数以及添加课程成员,不知道该如何优化,以及输出成绩之类的都得加上一定的语句,感觉添加课程需要修改大量的代码,希望在以后的学习当中能解决这个问题。

  • 写回答

2条回答 默认 最新

  • threenewbee 2023-05-13 14:00
    关注

    你想问什么问题,感觉你在分享技术文章?建议你去博客板块

    评论

报告相同问题?

问题事件

  • 创建了问题 5月13日

悬赏问题

  • ¥15 AnolisOs7.9如何安装 Qt_5.14.2的运行库
  • ¥20 求:怎么实现qt与pcie通信
  • ¥50 前后端数据顺序不一致问题,如何解决?(相关搜索:数据结构)
  • ¥15 基于蒙特卡罗法的中介效应点估计代码
  • ¥15 罗技G293和UE5.3
  • ¥20 Tesla 特斯拉K80显卡 如果需要使用该设备,你需要禁用系统上的另一个设备。
  • ¥30 QT调用百度智能云千帆模型无法取得返回文本
  • ¥50 CCD工业视觉相机检测出现光边
  • ¥60 二次元手游日常任务自动化代肝(相关搜索:自动化)
  • ¥15 mysql将查询的结果作为动态列名怎么实现