设计一个学生类,包含以下成员变量和方法: 。成员变量:姓名、年龄、性别 学号、数学成绩、语文成绩、英语成绩; 口方法:构造西数、析构西数、获取总分数的方法、获取平均分数的方法。 口请编写程序计算三位同学的总分和平均分。
时间: 2023-12-21 14:06:06 浏览: 78
以下是一个简单的C++代码示例:
```c++
#include<iostream>
#include<string>
using namespace std;
class Student{
private:
string name;
int age;
string gender;
string id;
double mathScore;
double chineseScore;
double englishScore;
public:
// 构造函数
Student(string n, int a, string g, string i, double m, double c, double e){
name = n;
age = a;
gender = g;
id = i;
mathScore = m;
chineseScore = c;
englishScore = e;
}
// 析构函数
~Student(){
cout << "Student " << name << " is destroyed." << endl;
}
// 获取总分数的方法
double getTotalScore(){
return mathScore + chineseScore + englishScore;
}
// 获取平均分数的方法
double getAverageScore(){
return (mathScore + chineseScore + englishScore) / 3.0;
}
};
int main(){
// 创建三个学生实例
Student s1("Tom", 18, "male", "001", 80, 90, 85);
Student s2("Alice", 17, "female", "002", 90, 80, 95);
Student s3("Bob", 19, "male", "003", 70, 75, 80);
// 计算总分和平均分并输出
cout << "Total scores: " << s1.getTotalScore() + s2.getTotalScore() + s3.getTotalScore() << endl;
cout << "Average scores: " << (s1.getAverageScore() + s2.getAverageScore() + s3.getAverageScore()) / 3.0 << endl;
return 0;
}
```
输出结果:
```
Total scores: 670
Average scores: 81.6667
Student Bob is destroyed.
Student Alice is destroyed.
Student Tom is destroyed.
```
阅读全文