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细节实现前不能定义该类对象。还有本程序也有一定弊端,就是如果我想添加课程数量,那么也就得修改构造函数以及添加课程成员,不知道该如何优化,以及输出成绩之类的都得加上一定的语句,感觉添加课程需要修改大量的代码,希望在以后的学习当中能解决这个问题。

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