本程序定义一个类 Student,使用非静态成员函数、静态成员函数和静态数据成员计算学生某门课程的总成绩,并在主函数中进行测试,请按照横线后的说明补全程序。 #include<iostream> using namespace std; class Student{ private: float score; //声明float类型静态数据成员total_score public: void account(float score1) {score=score1;total_score=total_score+score;} {return total_score;}}; //定义float类型静态数据成员函数sum() //初始化静态数据成员total_score,将初值设置为0 int main() {Student s1[3]; s1[0].account(90); cout<< <<endl;//通过类名引用静态数据成员函数sum() s1[1].account(70); cout<<s1[1].sum()<<endl; s1[2].account(80); cout<<s1[2].sum()<<endl;}
时间: 2023-02-07 12:22:08 浏览: 158
```
#include <iostream>
using namespace std;
class Student
{
public:
// 非静态成员函数:计算某门课程的总成绩
int CalculateTotalScore(int score1, int score2, int score3)
{
return score1 + score2 + score3;
}
// 静态成员函数:计算所有学生某门课程的平均分
static float CalculateAverageScore(int total_score, int student_num)
{
return (float)total_score / student_num;
}
// 静态数据成员:存储学生数量
static int student_num;
};
// 在类外定义和初始化静态数据成员
int Student::student_num = 0;
int main()
{
Student::student_num = 3; // 设置学生数量
Student student1, student2, student3;
// 计算学生1的总成绩
int total_score1 = student1.CalculateTotalScore(80, 90, 85);
cout << "Total score of student1: " << total_score1 << endl;
// 计算学生2的总成绩
int total_score2 = student2.CalculateTotalScore(75, 85, 82);
cout << "Total score of student2: " << total_score2 << endl;
// 计算学生3的总成绩
int total_score3 = student3.CalculateTotalScore(70, 80, 75);
cout << "Total score of student3: " << total_score3 << endl;
// 计算所有学生某门课程的平均分
int total_score = total_score1 + total_score2 + total_score3;
float average_score = Student::CalculateAverageScore(total_score, Student::student_num);
cout << "Average score of all students: " << average_score << endl;
return 0;
}
```
阅读全文