题:
定义一个Student类,在该类定义中包括:两个数据成员name(学生姓名)和score(分数);两个静态数据成员total(总分)和count(学生人数)。成员函数scoretotalcount(float s)用于设置每个学生的分数;静态成员函数sum()用于返回总分;静态成员函数average()用于求分数平均值。在main函数中,输入某班同学的成绩(班级学生人数随个数录入情况自动增加),并调用上述函数求全班学生的总分和平均分。``
#include<iostream>
using namespace std;
class Student
{
public:
Student(string);
static float total;
static int count;//定义静态数据成员
void scoretotalcount(float s);
static float sum();
static float average();//定义静态成员函数
private:
string name;
float score;
};
int Student::count = 0;
float Student::total = 0;
Student::Student(string name)
{
this->name = name;
count++;
}//录入学生姓名
void Student::scoretotalcount(float s)
{
score = s;
total += s;
}//录入学生成绩并随录入计算总分
float Student::sum()
{
return total;
}//返回全班总分
float Student::average()
{
return total / count;
}//返回全班平均分
int main()
{
Student student1("xiaowang");
student1.scoretotalcount(90.5);
Student student2("xiaoming");
student2.scoretotalcount(93.5);
cout << "全班总分为:" << Student::sum() << endl;
cout << "全班平均分为:" << Student::average() << endl;
return 0;
}