定义一个描述学生基本情况的类,数据成员包括姓名、学号、C++、英语和数学成绩,成员函数包括输出数据,姓名和学号、三门课的成绩,求出总成绩和平均成绩。
时间: 2024-05-14 18:15:17 浏览: 63
设计一个学生类Student(学生学号、姓名、数学、英语、计算机成绩;)
5星 · 资源好评率100%
```C++
#include <iostream>
using namespace std;
class Student {
private:
string name;
string id;
int score_c;
int score_e;
int score_m;
public:
void setName(string name);
void setId(string id);
void setScore(int c, int e, int m);
void displayBasicInfo();
void displayScores();
void displayTotalAndAverage();
};
void Student::setName(string name) {
this->name = name;
}
void Student::setId(string id) {
this->id = id;
}
void Student::setScore(int c, int e, int m) {
score_c = c;
score_e = e;
score_m = m;
}
void Student::displayBasicInfo() {
cout << "姓名:" << name << endl;
cout << "学号:" << id << endl;
}
void Student::displayScores() {
cout << "C语言成绩:" << score_c << endl;
cout << "英语成绩:" << score_e << endl;
cout << "数学成绩:" << score_m << endl;
}
void Student::displayTotalAndAverage() {
int total = score_c + score_e + score_m;
double average = total / 3.0;
cout << "总成绩:" << total << endl;
cout << "平均成绩:" << average << endl;
}
int main() {
Student stu;
stu.setName("张三");
stu.setId("20210001");
stu.setScore(90, 80, 85);
stu.displayBasicInfo();
stu.displayScores();
stu.displayTotalAndAverage();
return 0;
}
```
阅读全文