设计一个学生类,包含以下成员变量和方法: 成员变量:姓名、年龄、性别、学号、数学成绩、语文成绩、英语成绩;□方法:构造函数、析构函数、获取总分数的方法、获取平均分数的方法。□请编写程序计算三位同学的总分和平均分。
时间: 2023-10-20 16:16:13 浏览: 58
下面是一个可能的解法:
```cpp
#include <iostream>
using namespace std;
class Student {
private:
string name;
int age;
char gender;
string id;
float mathScore;
float chineseScore;
float englishScore;
public:
// 构造函数
Student(string name_, int age_, char gender_, string id_,
float mathScore_, float chineseScore_, float englishScore_) {
name = name_;
age = age_;
gender = gender_;
id = id_;
mathScore = mathScore_;
chineseScore = chineseScore_;
englishScore = englishScore_;
}
// 析构函数
~Student() {}
// 获取总分数
float getTotalScore() {
return mathScore + chineseScore + englishScore;
}
// 获取平均分数
float getAverageScore() {
return getTotalScore() / 3;
}
};
int main() {
Student s1("Alice", 20, 'F', "1001", 90, 85, 95);
Student s2("Bob", 21, 'M', "1002", 80, 70, 75);
Student s3("Charlie", 19, 'M', "1003", 85, 95, 90);
cout << s1.getTotalScore() << " " << s1.getAverageScore() << endl;
cout << s2.getTotalScore() << " " << s2.getAverageScore() << endl;
cout << s3.getTotalScore() << " " << s3.getAverageScore() << endl;
return 0;
}
```
输出:
```
270 90
225 75
270 90
```
说明:三位同学的总分和平均分分别为:Alice 270/90分,Bob 225/75分,Charlie 270/90分。