题目:
声明一个Student类,在该类中包括一个数据成员score(分数)、两个静态数据成员total_score(总分)和count(学生人数);还包括一个成员函数account()用于设置分数、累计学生的成绩之和、累计学生人数,一个静态成员函数sum()用于返回学生的成绩之和,另一个静态成员函数average()用于求全班成绩的平均值。在main函数中,输入某班同学的成绩,并调用上述函数求出全班学生的成绩之和和平均分。
#include<iostream>
using namespace std;
class Student
{
private:
float score;
static float total_score; //说明静态数据成员
static int count; //说明静态数据成员
public:
void put_score(float x){score=x;};
float get_score(){return score;};
void account(float s);
static float sum(); //说明静态函数成员
static float average(); //说明静态函数成员
};
float Student::total_score=0; //定义total_score
int Student::count=0; //定义count
void Student::account(float s)
{
score=s;
total_score+=score;
count++;
}
float Student::sum() //返回学生成绩之和
{
return total_score;
}
float Student::average() //计算平均成绩
{
return total_score/count;
}
int main()
{
Student student[100];
int i,n;
float s;
cout<<"请输入学生总数:";
cin>>n;
cout<<"请输入学生成绩:";
for(i=0;i<n;i++)
{
cin>>s;
student[i].account(s);
}
cout.precision(2);
cout.setf(ios::showpoint|ios::fixed);
cout<<"该班总分数为 "<<Student::sum()<<endl;
cout<<"该班平均分为 "<<Student::average()<<endl;
return 0;
}