标题:
C++语言静态数据成员与静态成员函数
[打印本页]
作者:
daming
时间:
2014-12-30 02:00
标题:
C++语言静态数据成员与静态成员函数
本帖最后由 daming 于 2014-12-30 02:15 编辑
#include<iostream>
using namespace std;
class Cat
{
private:
static int HowManyCats;
public:
Cat();
~Cat();
static int GetHowMany();
};
int Cat::HowManyCats=0;
Cat::Cat()
{
HowManyCats++;
}
Cat::~Cat()
{
HowManyCats--;
}
int Cat::GetHowMany()
{
return HowManyCats;
}
void main()
{
Cat a1,a2,a3;
cout<<Cat::GetHowMany()<<endl;
{
Cat a4,a5,a6;
cout<<a4.GetHowMany()<<endl;
}
cout<<a1.GetHowMany()<<endl;
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1