用c++语言编写程序,设计一个学生类,包含以下成员变量和方法: 成员变量:姓名、年龄、性别、学号、数学成绩、语文成绩、英语成绩; 方法:构造函数、析构函数、获取总分数的方法、获取平均分数的方法。请编写程序计算三位同学的总分和平均分。
时间: 2023-10-20 18:20:08 浏览: 189
以下是C++语言编写的程序:
```c++
#include<iostream>
#include<string>
using namespace std;
class Student {
private:
string name;
int age;
char gender;
string stu_num;
int math_score;
int chinese_score;
int english_score;
public:
Student(string n, int a, char g, string num, int math, int chinese, int english) {
name = n;
age = a;
gender = g;
stu_num = num;
math_score = math;
chinese_score = chinese;
english_score = english;
}
~Student() {}
int getTotalScore() {
return math_score + chinese_score + english_score;
}
float getAverageScore() {
return (float)(math_score + chinese_score + english_score) / 3;
}
};
int main() {
Student stu1("Tom", 18, 'M', "001", 90, 85, 95);
Student stu2("Lucy", 19, 'F', "002", 80, 90, 95);
Student stu3("Jerry", 20, 'M', "003", 85, 87, 88);
cout << "Total score of " << stu1.name << " is " << stu1.getTotalScore() << endl;
cout << "Average score of " << stu1.name << " is " << stu1.getAverageScore() << endl;
cout << "Total score of " << stu2.name << " is " << stu2.getTotalScore() << endl;
cout << "Average score of " << stu2.name << " is " << stu2.getAverageScore() << endl;
cout << "Total score of " << stu3.name << " is " << stu3.getTotalScore() << endl;
cout << "Average score of " << stu3.name << " is " << stu3.getAverageScore() << endl;
return 0;
}
```
上述程序中,我们定义了一个名为`Student`的类,该类包含了姓名、年龄、性别、学号、数学成绩、语文成绩和英语成绩等成员变量,以及构造函数、析构函数、获取总分数和获取平均分数等方法。在主函数中,我们实例化了三个`Student`对象,分别为`stu1`、`stu2`和`stu3`,并调用了获取总分数和获取平均分数的方法,输出了每个学生的总分数和平均分数。
阅读全文