【正确答案】#include<iostream>
using namespace std;
class student
{public:
void scoretotalcount(double s)
{score=s;
total=total+score;
count++;}
static double sum()
{return total;}
static double average()
{return total/count;
}
private:
double score;
static double total;
static double count;
};
double student::total=0;
double student::count=0;
int main()
{int i, n; double s;
cout<<"请输入学生人数:";
cin>>n:
student stu;
for(i=1; i<=n; i++)
{cout<<"请输入第"<<i<<"个学生的分数:";
cin>>s;
stu.seoretotalcount(s);}
cout<<"总分:"<<student::sum()<<endl;
cout<<"平均分:"<<student::average()<<endl;
}
【答案解析】 C++中声明类的一般形式如下:
Class类名{
private:私有数据和函数
public:公有数据和函数
protected:保护数据和函数
}
C++中成员函数的定义如下:
返回类型类名::成员函数名(参数列表)
{
成员函数//内部实现
}