用动态分配数组实现:1定义一个score类:私有数据成员有:学号姓名平时成绩期末考试成绩总评成绩;成员函数有:类的构造函数score自定义其构造形式count计算总评成绩:按总评成绩=平时04期末成绩06ShowScore输出各个私有数据成员; 2主函数要求以不带初始化的对象数组和带初始化的动态对象数组分别构造3个对象并分别输出每个对象的私有数据。 3输出格式如下:201701101 Li Hon
#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/bKk0 著作权归作者所有。请勿转载和采集!