设计一个学生类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-11-27 07:49:10 浏览: 58
以下是实现题目要求的代码:
```C++
#include<iostream>
using namespace std;
class Student {
private:
long ID;
int grade1, grade2, grade3;
public:
void init(long id, int g1, int g2, int g3) {
ID = id;
grade1 = g1;
grade2 = g2;
grade3 = g3;
}
long getID() {
return ID;
}
int getGrade1() {
return grade1;
}
int getGrade2() {
return grade2;
}
int getGrade3() {
return grade3;
}
};
void print(Student stu[], int n) {
for (int i = 0; i < n; i++) {
cout << "学号:" << stu[i].getID() << ",成绩1:" << stu[i].getGrade1() << ",成绩2:" << stu[i].getGrade2() << ",成绩3:" << stu[i].getGrade3() << endl;
}
}
void find(Student stu[], long id) {
for (int i = 0; i < 3; i++) {
if (stu[i].getID() == id) {
cout << "学号:" << stu[i].getID() << ",成绩1:" << stu[i].getGrade1() << ",成绩2:" << stu[i].getGrade2() << ",成绩3:" << stu[i].getGrade3() << endl;
return;
}
}
cout << "未找到该学生" << endl;
}
void max(Student stu[], int n) {
int maxIndex = 0;
int maxGrade = stu[0].getGrade1() + stu[0].getGrade2() + stu[0].getGrade3();
for (int i = 1; i < n; i++) {
int gradeSum = stu[i].getGrade1() + stu[i].getGrade2() + stu[i].getGrade3();
if (gradeSum > maxGrade) {
maxIndex = i;
maxGrade = gradeSum;
}
}
cout << "总成绩最高的学生信息:" << endl;
cout << "学号:" << stu[maxIndex].getID() << ",成绩1:" << stu[maxIndex].getGrade1() << ",成绩2:" << stu[maxIndex].getGrade2() << ",成绩3:" << stu[maxIndex].getGrade3() << endl;
}
int main() {
const int n = 3;
Student stu[n];
stu[0].init(2021001, 80, 75, 90);
stu[1].init(2021002, 88, 92, 85);
stu[2].init(2021003, 92, 82, 87);
print(stu, n);
find(stu, 2021002);
max(stu, n);
return 0;
}
```
输出结果为:
```
学号:2021001,成绩1:80,成绩2:75,成绩3:90
学号:2021002,成绩1:88,成绩2:92,成绩3:85
学号:2021003,成绩1:92,成绩2:82,成绩3:87
学号:2021002,成绩1:88,成绩2:92,成绩3:85
总成绩最高的学生信息:
学号:2021003,成绩1:92,成绩2:82,成绩3:87
```
阅读全文