使用C++设计一个类该类用于存储学生的成绩包括:数学、物理、化学英文类的成员函数包括构造函数析构函数成绩录入成绩打印成绩排序等
以下是一个基本的实现:
#include <iostream>
#include <algorithm>
using namespace std;
class StudentScore {
private:
float mathScore;
float physicsScore;
float chemistryScore;
float englishScore;
public:
StudentScore() { // 构造函数,初始化成绩为0
mathScore = 0;
physicsScore = 0;
chemistryScore = 0;
englishScore = 0;
}
~StudentScore() { // 析构函数
cout << "StudentScore object destroyed" << endl;
}
void inputScore(float math, float physics, float chemistry, float english) { // 成绩录入
mathScore = math;
physicsScore = physics;
chemistryScore = chemistry;
englishScore = english;
}
void printScore() { // 成绩打印
cout << "Math score: " << mathScore << endl;
cout << "Physics score: " << physicsScore << endl;
cout << "Chemistry score: " << chemistryScore << endl;
cout << "English score: " << englishScore << endl;
}
void sortScore() { // 成绩排序
float scores[4] = {mathScore, physicsScore, chemistryScore, englishScore};
sort(scores, scores+4);
cout << "Scores in ascending order: ";
for (int i = 0; i < 4; i++) {
cout << scores[i] << " ";
}
cout << endl;
}
};
int main() {
StudentScore s;
s.inputScore(80, 75, 90, 85);
s.printScore();
s.sortScore();
return 0;
}
输出结果:
Math score: 80
Physics score: 75
Chemistry score: 90
English score: 85
Scores in ascending order: 75 80 85 90
StudentScore object destroyed
原文地址: https://www.cveoy.top/t/topic/qOX 著作权归作者所有。请勿转载和采集!