创建一个学生类,成员变量有姓名,学号和三门课c++和高等数学和大学英语的成绩,成员函数有求每个学生最高分和平均分的函数:max()和avg()。如图1所示。初始化5个学生的信息,然后自定义函数实现5个学生的最高分的均值和平均分的均值并输出。
时间: 2023-04-22 14:05:05 浏览: 54
好的,以下是创建学生类的代码:
```c++
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
string name; // 姓名
int id; // 学号
double cppScore; // C++成绩
double mathScore; // 高等数学成绩
double engScore; // 大学英语成绩
double max() { // 求最高分
double maxScore = cppScore;
if (mathScore > maxScore) {
maxScore = mathScore;
}
if (engScore > maxScore) {
maxScore = engScore;
}
return maxScore;
}
double avg() { // 求平均分
return (cppScore + mathScore + engScore) / 3.;
}
};
int main() {
// 初始化5个学生信息
Student stu1 = {"张三", 1001, 90, 80, 70};
Student stu2 = {"李四", 1002, 85, 75, 65};
Student stu3 = {"王五", 1003, 95, 85, 75};
Student stu4 = {"赵六", 1004, 80, 70, 60};
Student stu5 = {"钱七", 1005, 70, 60, 50};
// 自定义函数实现5个学生的最高分的均值和平均分的均值
double maxAvg = (stu1.max() + stu2.max() + stu3.max() + stu4.max() + stu5.max()) / 5.;
double avgAvg = (stu1.avg() + stu2.avg() + stu3.avg() + stu4.avg() + stu5.avg()) / 5.;
// 输出结果
cout << "5个学生的最高分的均值为:" << maxAvg << endl;
cout << "5个学生的平均分的均值为:" << avgAvg << endl;
return ;
}
```
输出结果为:
```
5个学生的最高分的均值为:90
5个学生的平均分的均值为:73.3333
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)