创建student类,在main函数中输入3个学生数据,包括学号,姓名和成绩,要求只输出成绩在80~90分数段的学生数据。 具体要求如下: (1)数据成员设定成private (2)成员函数设定成public (3)将类声明和成员函数的定义分开,放到.h和.cpp中
时间: 2024-01-02 11:04:33 浏览: 71
好的,我已经理解了您的问题。下面是student类的.h文件和.cpp文件的代码:
student.h文件:
```
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
class student {
private:
int id;
std::string name;
float score;
public:
student(int id, std::string name, float score);
void print();
};
#endif
```
student.cpp文件:
```
#include "student.h"
#include <iostream>
student::student(int id, std::string name, float score) {
this->id = id;
this->name = name;
this->score = score;
}
void student::print() {
if (score >= 80 && score <= 90) {
std::cout << "id: " << id << ", name: " << name << ", score: " << score << std::endl;
}
}
```
在main函数中,您可以这样输入3个学生数据:
```
#include "student.h"
int main() {
student s1(1, "Tom", 85);
student s2(2, "Jerry", 95);
student s3(3, "Mike", 75);
s1.print();
s2.print();
s3.print();
return 0;
}
```
输出结果:
```
id: 1, name: Tom, score: 85
```
阅读全文