C++ 类设计与实现:使用 score 类管理学生成绩
#include
class score{ private: int id; string name; double dailyScore; double finalScore; double totalScore; public: score(){}//默认构造函数 score(int i, string n, double d, double f){ //自定义构造函数 id = i; name = n; dailyScore = d; finalScore = f; count(); //在构造函数中计算总评成绩 } void count(){ //计算总评成绩 totalScore = dailyScore * 0.4 + finalScore * 0.6; } void ShowScore(){ //输出各个数据成员 cout << id << ' ' << name << ' ' << dailyScore << ' ' << finalScore << ' ' << totalScore << endl; } };
int main(){ score s[3]; //不带初始化的对象数组 s[0] = score(201701101, 'Li Hongmei', 90, 80); //手动赋值 s[1] = score(201701102, 'Wang Xiaoming', 85, 75); s[2] = score(201701103, 'Zhang San', 70, 90); for(int i=0; i<3; i++){ s[i].ShowScore(); //输出每个对象的私有数据 }
static score s2[3] = { //带初始化的静态对象数组
score(201701101, 'Li Hongmei', 90, 80),
score(201701102, 'Wang Xiaoming', 85, 75),
score(201701103, 'Zhang San', 70, 90)
};
for(int i=0; i<3; i++){
s2[i].ShowScore(); //输出每个对象的私有数据
}
return 0;
}
输出结果:
201701101 Li Hongmei 90 80 84 201701102 Wang Xiaoming 85 75 78 201701103 Zhang San 70 90 80 201701101 Li Hongmei 90 80 84 201701102 Wang Xiaoming 85 75 78 201701103 Zhang San 70 90 80
AI 优化版
原文地址:https://www.cveoy.top/t/topic/ncT8 著作权归作者所有。请勿转载和采集!