6.04-静态成员函数.cpp

来自「Visual C++的课件」· C++ 代码 · 共 44 行

CPP
44
字号
//Listing 14.4 static data members

#include <iostream.h>

class Cat
{
public:
	Cat(int age):itsAge(age){HowManyCats++; }
	virtual ~Cat() { HowManyCats--; }
	virtual int GetAge() { return itsAge; }
	virtual void SetAge(int age) { itsAge = age; }
	static int GetHowMany() { return HowManyCats; }
private:
	int itsAge;
	static int HowManyCats;
};

int Cat::HowManyCats = 0;

void TelepathicFunction();

int main()
{
	const int MaxCats = 5;
	Cat *CatHouse[MaxCats]; int i;
	for (i = 0; i<MaxCats; i++)
	{
		CatHouse[i] = new Cat(i);
		TelepathicFunction();
	}

	for ( i = 0; i<MaxCats; i++)
	{
		delete CatHouse[i];
		TelepathicFunction();
	}
	return 0;
}

void TelepathicFunction()
{
	cout << "There are " << Cat::GetHowMany() << " cats alive!\n";
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?