用c++定义学生成绩类,显示学生的学号,数学成绩和语文成绩,实现成绩输入正确性验证,isScoreValid()判断成绩,disply()函数显示成绩,成绩小于0或大于100时,提示数据有误。)
时间: 2024-01-03 10:05:18 浏览: 15
好的,以下是C++定义学生成绩类的代码:
```cpp
#include <iostream>
#include <string>
using namespace std;
class StudentScore {
private:
string studentID;
float mathScore;
float chineseScore;
public:
StudentScore(string id, float math, float chinese) {
studentID = id;
mathScore = math;
chineseScore = chinese;
}
bool isScoreValid(float score) {
return score >= 0 && score <= 100;
}
void display() {
cout << "学号:" << studentID << endl;
if (isScoreValid(mathScore)) {
cout << "数学成绩:" << mathScore << endl;
} else {
cout << "数学成绩数据有误!" << endl;
}
if (isScoreValid(chineseScore)) {
cout << "语文成绩:" << chineseScore << endl;
} else {
cout << "语文成绩数据有误!" << endl;
}
}
};
```
在这个类中,我们定义了一个`StudentScore`类,其中包含学生的学号、数学成绩和语文成绩。在构造函数中,我们将这些属性初始化。
我们还实现了一个`isScoreValid`函数,用于判断成绩是否在0到100之间。如果成绩无效,则在`display`函数中输出错误信息。
最后,我们定义了一个`display`函数,用于显示学生的学号和成绩。如果成绩无效,则输出错误信息。
希望这个代码对您有所帮助!