C++定义一个学生类,成员变量包括学号和四门课的成绩

2024-11-17 06:38:34
推荐回答(1个)
回答1:

#include
#include
using namespace std;

class Student {
private:
enum { SIZE = 5 };
string SName;
string SNo;
double Score[SIZE];
public:
Student(){}
Student(string name, string no, double sc[]);
void Show() const;
double GetAverage() const { return Score[SIZE - 1]; }
double GetOptimumScore() const;
};

Student::Student(string name, string no, double sc[]) {
SName = name;
SNo = no;
Score[SIZE - 1] = 0.0;
for(int i = 0; i < SIZE - 1; ++i) {
Score[i] = sc[i];
Score[SIZE - 1] += sc[i];
}
Score[SIZE - 1] = Score[SIZE - 1]/(SIZE - 1);
}

void Student::Show() const {
cout << SName << "\t";
cout << SNo << "\t";
cout.precision();
cout.showpoint;
for(int i = 0; i < SIZE; ++i) cout << Score[i] << "\t";
cout << endl;
}

double Student::GetOptimumScore() const {
double max = 0.0;
for(int i = 0; i < SIZE - 1; ++i)
if(Score[i] > max) max = Score[i];
return max;
}

int main() {
Student s[5];
string name,no;
double score[5];
for(int i = 0; i < 5; ++i) {
cout << "学生" << i + 1 << "信息\n";
cout << "姓名 : ";
cin >> name;
cout << "学号 : " ;
cin >> no;
cout << "高等数学 : ";
cin >> score[0];
cout << "英语 : ";
cin >> score[1];
cout << "C编程 : ";
cin >> score[2];
cout << "电脑硬件 : ";
cin >> score[3];
cout << endl;
s[i] = Student(name,no,score);
}
cout << "姓名\t学号\t高数\t英语\t编程\t硬件\t\n";
for(int i = 0; i < 48; ++i) cout << "*";
cout << endl;
for(int i = 0; i < 5; ++i) s[i].Show();
cout << endl;
return 0;
}
请采纳。