c++创建一个对象数组,数组的元素是学生对象,学生的信息包括学号,姓名和成绩,在main函数种将所有成绩大于80分的学生的信息显示出来
时间: 2024-01-04 13:03:17 浏览: 101
#include <iostream>
#include <string>
using namespace std;
class Student {
public:
int num;
string name;
int score;
Student(int num, string name, int score) {
this->num = num;
this->name = name;
this->score = score;
}
};
int main() {
Student students[] = {
Student(1, "Tom", 85),
Student(2, "Jerry", 75),
Student(3, "Mary", 90),
Student(4, "John", 80),
Student(5, "Lisa", 95)
};
int len = sizeof(students) / sizeof(Student);
for (int i = 0; i < len; i++) {
if (students[i].score > 80) {
cout << "学号:" << students[i].num << ",姓名:" << students[i].name << ",成绩:" << students[i].score << endl;
}
}
return 0;
}
阅读全文