2.创建student类,在main函数中输入3个学生数据,包括学号,姓名和成绩,要求只输出成绩在80~90分数段的学生数据。 具体要求如下: (1)数据成员设定成private (2)成员函数设定成public (3)将类声明和成员函数的定义分开,放到.h和.cpp中
时间: 2024-01-02 13:03:24 浏览: 15
C++输入函数和成绩显示函数,并计算每位同学的总分和平均分,对成绩排名输出等.pdf
student.h文件:
```cpp
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class Student {
public:
Student(int id, std::string name, float score);
int getId() const;
std::string getName() const;
float getScore() const;
private:
int id_;
std::string name_;
float score_;
};
#endif // STUDENT_H
```
student.cpp文件:
```cpp
#include "student.h"
Student::Student(int id, std::string name, float score)
: id_(id), name_(name), score_(score) {}
int Student::getId() const {
return id_;
}
std::string Student::getName() const {
return name_;
}
float Student::getScore() const {
return score_;
}
```
main.cpp文件:
```cpp
#include <iostream>
#include <vector>
#include "student.h"
int main() {
std::vector<Student> students = {
Student(1, "Tom", 90),
Student(2, "Jerry", 85),
Student(3, "Alice", 75),
};
for (const auto& student : students) {
float score = student.getScore();
if (score >= 80 && score <= 90) {
std::cout << "id: " << student.getId()
<< ", name: " << student.getName()
<< ", score: " << score << std::endl;
}
}
return 0;
}
```
阅读全文