標(biāo)題:
C++語言靜態(tài)數(shù)據(jù)成員與靜態(tài)成員函數(shù)
[打印本頁]
作者:
daming
時間:
2014-12-30 02:00
標(biāo)題:
C++語言靜態(tài)數(shù)據(jù)成員與靜態(tài)成員函數(shù)
本帖最后由 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;
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1