1、设计一个学生类student,数据成员为学号(long型)和三门课程成绩,函数有:init函数初始化数据,get函数返回数据 2、编写函数 void print(student stu[],int n) { 输出每个学生的信息,每个学生一行 } void find(student stu[],long id) { 查找学号为id的学生成绩,并输出 } void max(student stu[],int n) { 输出总成绩最高的学生信息 } 3、编写主函数,创建student对象数组,长度自定义,为对象数组赋值,然后调用 2、中的函数完成功能
时间: 2023-12-18 08:03:16 浏览: 88
以下是实现学生类student和相关函数的代码:
```c++
#include <iostream>
using namespace std;
class student {
private:
long id;
double score1, score2, score3;
public:
void init(long i, double s1, double s2, double s3) {
id = i;
score1 = s1;
score2 = s2;
score3 = s3;
}
long getId() { return id; }
double getScore1() { return score1; }
double getScore2() { return score2; }
double getScore3() { return score3; }
};
void print(student stu[], int n) {
for (int i = 0; i < n; i++) {
cout << "学号:" << stu[i].getId() << ",成绩:" << stu[i].getScore1() << " "
<< stu[i].getScore2() << " " << stu[i].getScore3() << endl;
}
}
void find(student stu[], long id) {
for (int i = 0; i < 3; i++) {
if (stu[i].getId() == id) {
cout << "学号:" << stu[i].getId() << ",成绩:" << stu[i].getScore1() << " "
<< stu[i].getScore2() << " " << stu[i].getScore3() << endl;
break;
}
}
}
void max(student stu[], int n) {
double maxSum = 0;
int maxIndex = 0;
for (int i = 0; i < n; i++) {
double sum = stu[i].getScore1() + stu[i].getScore2() + stu[i].getScore3();
if (sum > maxSum) {
maxSum = sum;
maxIndex = i;
}
}
cout << "总成绩最高的学生信息:学号:" << stu[maxIndex].getId() << ",成绩:"
<< stu[maxIndex].getScore1() << " " << stu[maxIndex].getScore2() << " "
<< stu[maxIndex].getScore3() << endl;
}
int main() {
student stu[3];
stu[0].init(1001, 90, 80, 70);
stu[1].init(1002, 80, 70, 60);
stu[2].init(1003, 70, 60, 50);
print(stu, 3);
find(stu, 1002);
max(stu, 3);
return 0;
}
```
在主函数中创建了一个长度为3的student对象数组,并为每个对象赋值,然后调用print、find和max函数完成相应功能。
阅读全文