定义一个student类,在该类定义中包括:一个数据成员score(分数)及两个静态数据成员total(总分)和学生人数count;成员函数scoretotalcount(double s)用于设置分数、求总分和累计学生人数;静态成员函数sum()用于返回总分;静态成员函数average()用于求平在main函数中,输入某班同学的成绩,并调用上述函数求全班学生的总分和平均分。
 
【正确答案】#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++中成员函数的定义如下:
   返回类型类名::成员函数名(参数列表)
   {
   成员函数//内部实现
   }