C++建立一个对象数组,内放5个学生的数据(学号,成绩),设立一个函数max,用指针指向数组元首

2022-08-10 教育 69阅读
类似的,C++书上应该都有,有时间的话,把书上的代码敲一下。

#include

using namespace std;

class Student {
private:
int id;
float score;
public:
Student() {
id = 0;
score = 0;
}
Student(int id, float score) {
this->id = id;
this->score = score;
}
float getScore() {return score; }
int getID() {return id; }
};

void max(Student* s, int size) {
if (s == NULL || size < 1) return;
int l = 0;
for (int i = 1; i < size; i++) {
if (s[i].getScore() > s[l].getScore())
l = i;
}
cout << "Student with ID \"" << s[l].getID()
<< "\" has the largest grade." << endl;
}

int main() {
const int num = 5;
Student students[num] = {
Student(1, 78), Student(2, 92), Student(4, 81),
Student(4, 89), Student(5, 68)
};
max(students, 1);
return 0;
}
声明:你问我答网所有作品(图文、音视频)均由用户自行上传分享,仅供网友学习交流。若您的权利被侵害,请联系fangmu6661024@163.com