6.01-静态成员数据.cpp

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

CPP
41
字号
//Listing 14.1 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 HowManyCats;

private:
	int itsAge;

};

int Cat::HowManyCats = 0;

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

	for (i = 0; i<MaxCats; i++)
	{
		cout << "There are ";
		cout << Cat::HowManyCats;
		cout << " cats left!\n";
		cout << "Deleting the one which is ";
		cout << CatHouse[i]->GetAge();
		cout << " years old\n";
		delete CatHouse[i];
		CatHouse[i] = 0;
	}
	return 0;
}

⌨️ 快捷键说明

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