(1) 定义一个Student类,其中包括私有数据成员和公有成员函数,即 mId 学号 mMath 高等数学成绩 mEnglish 英语成绩 mProgramming 程序设计成绩 InScore() 输入学号和各科成绩,并且计算平均成绩 ShowScore() 输出学号和各科成绩 使用Student类,输入某班n(事先不能确定)个学生的学号和各科成绩,然后求各个学生的平均成绩,并列表输出学生的学号、各科成绩和平均成绩。
时间: 2023-05-23 20:03:24 浏览: 111
以下是定义的Student类的完整代码:
```
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string mId;
double mMath, mEnglish, mProgramming;
public:
void InScore() {
cout << "请输入学生学号:" << endl;
cin >> mId;
cout << "请输入高等数学成绩:" << endl;
cin >> mMath;
cout << "请输入英语成绩:" << endl;
cin >> mEnglish;
cout << "请输入程序设计成绩:" << endl;
cin >> mProgramming;
}
void ShowScore() {
cout << "学生学号:" << mId << endl;
cout << "高等数学成绩:" << mMath << endl;
cout << "英语成绩:" << mEnglish << endl;
cout << "程序设计成绩:" << mProgramming << endl;
}
double CalculateAverage() {
return (mMath + mEnglish + mProgramming) / 3.0;
}
};
```
下面是使用Student类来输入n个学生的信息并输出平均成绩的完整代码:
```
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<Student> students;
int n;
cout << "请输入学生总数:" << endl;
cin >> n;
for (int i = 0; i < n; i++) {
Student student;
student.InScore();
students.push_back(student);
}
for (int i = 0; i < n; i++) {
students[i].ShowScore();
double averageScore = students[i].CalculateAverage();
cout << "平均成绩:" << averageScore << endl;
}
return 0;
}
```