成员变量:姓名、年龄、性别、学号、数学成绩、语文成绩、英语成绩; 方法:构造函数、析构函数、获取总分数的方法、获取平均分数的方法。 请编写程序计算Tom同学的总分和平均分,假设Tom同学今年18岁, 男生, 学号是20220001, 语数外的成绩分别是90, 95, 80。
时间: 2023-06-19 17:05:42 浏览: 95
以下是一个可能的实现:
```cpp
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name;
int age;
char gender;
string id;
int mathScore;
int chineseScore;
int englishScore;
Student(string n, int a, char g, string i, int m, int c, int e) {
name = n;
age = a;
gender = g;
id = i;
mathScore = m;
chineseScore = c;
englishScore = e;
}
~Student() {}
int getTotalScore() {
return mathScore + chineseScore + englishScore;
}
double getAverageScore() {
return (mathScore + chineseScore + englishScore) / 3.0;
}
};
int main() {
Student tom("Tom", 18, 'M', "20220001", 90, 95, 80);
cout << "Tom's total score is: " << tom.getTotalScore() << endl;
cout << "Tom's average score is: " << tom.getAverageScore() << endl;
return 0;
}
```
输出结果:
```
Tom's total score is: 265
Tom's average score is: 88.3333
```
阅读全文