|  | 
 
| 
01.#include <iostream>
x
马上注册,结交更多好友,享用更多功能^_^您需要 登录 才可以下载或查看,没有账号?立即注册  02.using namespace std;
 03.class Student                   //定义Student类
 04.{
 05.public:
 06.   Student(int n,int a,float s):num(n),age(a),score(s){ }      //定义构造函数
 07.   void total( );
 08.   static float average( );      //声明静态成员函数
 09.private:
 10.   int num;
 11.   int age;
 12.   float score;
 13.   static float sum;            //静态数据成员
 14.   static int count;            //静态数据成员
 15.};
 16.void Student::total( )                      //定义非静态成员函数
 17.{
 18.   sum+=score;                            //累加总分
 19.   count++;                               //累计已统计的人数
 20.}
 21.float  Student::average( )                  //定义静态成员函数
 22.{
 23.   return(sum/count);
 24.}
 
 对于这句话:静态成员函数可以直接引用本类中的静态数据成员;
 上面的total 成员函数中的sum+=score;    count++;           不也是引用了静态数据成员吗
 
 
 | 
 |