定义一个表示学生信息的类Student,要求如下: (1)类Student的成员变量:学号、姓名、性别、年龄和Java成绩。 (2)类Student的构造方法。 (3)类Student的getters和setters方法。 (4)根据类Student的定义,创建5个该类的对象,输出根据成绩升序排序后每个学生的信息。
时间: 2023-03-28 12:01:26 浏览: 151
编写一个JAVA程序,定义一个Student类
5星 · 资源好评率100%
好的,我可以回答这个问题。以下是类Student的定义和实现:
```c
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Student {
private:
string id; // 学号
string name; // 姓名
string gender; // 性别
int age; // 年龄
int javaScore; // Java成绩
public:
Student(string id, string name, string gender, int age, int javaScore) {
this->id = id;
this->name = name;
this->gender = gender;
this->age = age;
this->javaScore = javaScore;
}
string getId() {
return id;
}
void setId(string id) {
this->id = id;
}
string getName() {
return name;
}
void setName(string name) {
this->name = name;
}
string getGender() {
return gender;
}
void setGender(string gender) {
this->gender = gender;
}
int getAge() {
return age;
}
void setAge(int age) {
this->age = age;
}
int getJavaScore() {
return javaScore;
}
void setJavaScore(int javaScore) {
this->javaScore = javaScore;
}
};
bool compare(Student s1, Student s2) {
return s1.getJavaScore() < s2.getJavaScore();
}
int main() {
Student s1("001", "张三", "男", 18, 90);
Student s2("002", "李四", "女", 19, 80);
Student s3("003", "王五", "男", 20, 70);
Student s4("004", "赵六", "女", 21, 60);
Student s5("005", "钱七", "男", 22, 50);
Student students[] = {s1, s2, s3, s4, s5};
int n = sizeof(students) / sizeof(students[]);
sort(students, students + n, compare);
for (int i = ; i < n; i++) {
cout << "学号:" << students[i].getId() << ",姓名:" << students[i].getName() << ",性别:" << students[i].getGender() << ",年龄:" << students[i].getAge() << ",Java成绩:" << students[i].getJavaScore() << endl;
}
return ;
}
```
输出结果如下:
```
学号:005,姓名:钱七,性别:男,年龄:22,Java成绩:50
学号:004,姓名:赵六,性别:女,年龄:21,Java成绩:60
学号:003,姓名:王五,性别:男,年龄:20,Java成绩:70
学号:002,姓名:李四,性别:女,年龄:19,Java成绩:80
学号:001,姓名:张三,性别:男,年龄:18,Java成绩:90
```
阅读全文