C++ 用动态分配数组实现 Score 类:计算总评成绩并输出学生信息
#include
class score { private: string id; // 学号 string name; // 姓名 double daily; // 平时成绩 double exam; // 期末考试成绩 double total; // 总评成绩 public: score(string id = "", string name = "", double daily = 0.0, double exam = 0.0, double total = 0.0) { this->id = id; this->name = name; this->daily = daily; this->exam = exam; this->total = total; count(); } void count() { total = daily * 0.4 + exam * 0.6; } void ShowScore() { cout << id << " " << name << " " << daily << " " << exam << " " << total << endl; } };
int main() { score s1("201701101", "Li Hongmei", 90, 80); s1.ShowScore();
score s2("201701102", "Zhang San", 85, 90);
s2.ShowScore();
score s3("201701103", "Wang Wu", 75, 70);
s3.ShowScore();
int n;
cout << "Enter the number of students: ";
cin >> n;
score *s = new score[n];
for (int i = 0; i < n; i++) {
string id, name;
double daily, exam;
cout << "Enter the information of student " << i+1 << ": ";
cin >> id >> name >> daily >> exam;
s[i] = score(id, name, daily, exam);
}
for (int i = 0; i < n; i++) {
s[i].ShowScore();
}
delete [] s;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/ncRT 著作权归作者所有。请勿转载和采集!