一个简单的C加加有关于类的问题

C++ 中的类(Class)可以看做C语言中结构体(Struct)的升级版。结构体是一种构造类型,可以包含若干成员变量,每个成员变量的类型可以不同;可以通过结构体来定义结构体变量,每个变量拥有相同的性质。

img

参考


你题目的解答代码如下:

#include <iostream>
#include <string>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <cstdlib>
using namespace std;
 
class Student {
    // 在此处补充你的代码
public:
 
    char name[20];
    char c;
    int age, studentID;
    double first, second, third, fouth, average;
    void input()
    {
        cin.getline(name, 20, ',');
        cin >> age >> c >> studentID >> c;
        cin >> first >> c >> second >> c >> third >> c >> fouth;
    }
    void calculate()
    {
        average = (first + second + third + fouth) / 4;
    }
    void output()
    {
        cout << name << ',';
        cout << age << ',' << studentID << ',' << average << endl;
    }
};
 
int main() {
    Student student;        // 定义类的对象
    student.input();        // 输入数据
    student.calculate();    // 计算平均成绩
    student.output();       // 输出数据
}

如有帮助,请点击我的回答下方的【采纳该答案】按钮帮忙采纳下,谢谢!

img