aiaiask.com©

开花即结果 提问即答案

"C++编程:定义Student类实现学生成绩管理与统计功能"
原始问题:

"C++编程:定义Student类实现学生成绩管理与统计功能"

以下是定义一个Student类的代码:

#include <iostream>
using namespace std;

class Student {
private:
    float score;
    static float total;
    static int count;
public:
    void scoretotalcount(float score) {
        this->score = score;
        total += score;
        count++;
    }
    float getScore() {
        return score;
    }
    static float sum() {
        return total;
    }
    static float average() {
        return total / count;
    }
};

float Student::total = 0;
int Student::count = 0;

int main() {
    Student students[5];
    float score;
    for (int i = 0; i < 5; i++) {
        cout << "请输入第" << i+1 << "个学生的成绩:";
        cin >> score;
        students[i].scoretotalcount(score);
    }
    cout << "全班学生的分数为:";
    for (int i = 0; i < 5; i++) {
        cout << students[i].getScore() << " ";
    }
    cout << endl;
    cout << "全班学生的总分为:" << Student::sum() << endl;
    cout << "全班学生的平均分为:" << Student::average() << endl;
    return 0;
}

在main函数中,首先定义了一个包含5个元素的Student数组,然后通过循环输入每个学生的成绩,并调用scoretotalcount方法设置分数、求总分及累计学生人数。接着,通过循环调用每个学生的getter方法获取分数,并输出全班学生的分数。最后,通过调用静态成员函数sum和average分别输出全班学生的总分和平均分。

"C++编程:定义Student类实现学生成绩管理与统计功能"
https://www.aiaiask.com/ask/37207.html
AI / 模型Siliconflow
时间 / DATEJune 17, 2023, 2:03 PM
语言zh-CN
IP / 区域辽宁 沈阳