#include <iostream>
using namespace std;
class counter
{
static long count;//私有的静态数据成员
public:
counter(){count++;}
long get_count(){return count;}
static long get_count_l(){return count;}
/*采用公有的成员函数读取私有静态数据,*/
~counter(){count--;}
};
//long count=5;
long counter::count=5;
counter c1,c2,c3;
void main()
{
cout<<"(1)对象的count值="<<c3.get_count()<<endl;
counter c4;
cout<<"(2)对象的count值="<<c3.get_count()<<endl;
cout<<"(3)对象的count值="<<c4.get_count()<<endl;
cout<<"采用静态成员函数来读取静态私有数据"<<endl;
cout<<"(1)对象的count值="<<counter::get_count_l()<<endl;
counter c5;
cout<<"(2)对象的count值="<<counter::get_count_l()<<endl;
cout<<"(3)对象的count值="<<counter::get_count_l()<<endl;
}
/*静态数据成员是种特殊的数据成员,它存放在内存空间内的值始终保留,只要程序一开始运行它便生成,程序运行结束才消失
其空间并不随着对象的创建而分配。或随着对象的消失而回收。所以不管是公有的,私有的还是保护的,都必须在所有函数体外进行初始化。
初始化的格式:<类型> 类名::静态数据成员名=初值*/
using namespace std;
class counter
{
static long count;//私有的静态数据成员
public:
counter(){count++;}
long get_count(){return count;}
static long get_count_l(){return count;}
/*采用公有的成员函数读取私有静态数据,*/
~counter(){count--;}
};
//long count=5;
long counter::count=5;
counter c1,c2,c3;
void main()
{
cout<<"(1)对象的count值="<<c3.get_count()<<endl;
counter c4;
cout<<"(2)对象的count值="<<c3.get_count()<<endl;
cout<<"(3)对象的count值="<<c4.get_count()<<endl;
cout<<"采用静态成员函数来读取静态私有数据"<<endl;
cout<<"(1)对象的count值="<<counter::get_count_l()<<endl;
counter c5;
cout<<"(2)对象的count值="<<counter::get_count_l()<<endl;
cout<<"(3)对象的count值="<<counter::get_count_l()<<endl;
}
/*静态数据成员是种特殊的数据成员,它存放在内存空间内的值始终保留,只要程序一开始运行它便生成,程序运行结束才消失
其空间并不随着对象的创建而分配。或随着对象的消失而回收。所以不管是公有的,私有的还是保护的,都必须在所有函数体外进行初始化。
初始化的格式:<类型> 类名::静态数据成员名=初值*/