#include #include using namespace std;

class score{ private: int num; //学号 string name; //姓名 float dailyScore; //平时成绩 float finalScore; //期末考试成绩 float totalScore; //总评成绩 public: score(int n, string na, float d, float f){ //构造函数 num = n; name = na; dailyScore = d; finalScore = f; count(); //计算总评成绩 } void count(){ //计算总评成绩 totalScore = dailyScore * 0.4 + finalScore * 0.6; } void ShowScore(){ //输出各个私有数据成员 cout << num << ' ' << name << ' ' << dailyScore << ' ' << finalScore << ' ' << totalScore << endl; } };

int main(){ score s1(201701101, 'Li Hongmei', 90, 80); //构造对象1 score s2(201701102, 'Wang Xiaoming', 85, 95); //构造对象2 score s3(201701103, 'Zhang San', 70, 60); //构造对象3

score *p = new score[2]{score(201701104, 'Liu Si', 80, 85), score(201701105, 'Zhao Wu', 75, 90)}; //动态对象数组

s1.ShowScore(); //输出对象1的私有数据
s2.ShowScore(); //输出对象2的私有数据
s3.ShowScore(); //输出对象3的私有数据

p[0].ShowScore(); //输出动态对象数组中第一个对象的私有数据
p[1].ShowScore(); //输出动态对象数组中第二个对象的私有数据

delete[] p; //释放动态对象数组内存

return 0;

}


原文地址: https://www.cveoy.top/t/topic/ncYw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录