#include using namespace std; //Cat类的定义与实现 class Cat { public: Cat(int age) : itsAge(age) { HowManyCats++; } ~Cat() { HowManyCats--; } int GetAge() const { return itsAge; } static int GetHowMany() { return HowManyCats; } private: int itsAge; static int HowManyCats; }; int Cat::HowManyCats = 0; //测试 int main() { Cat cat1(1); cout << "Cat1age=" << cat1.GetAge() << endl; cout << "There are " << Cat::GetHowMany() << " cats alive!" << endl; Cat cat2(2); cout << "Cat2age=" << cat2.GetAge() << endl; cout << "There are " << Cat::GetHowMany() << " cats alive!" << endl; Cat cat3(3); cout << "Cat3age=" << cat3.GetAge() << endl; cout << "There are " << Cat::GetHowMany() << " cats alive!" << endl; Cat cat4(4); cout << "Cat4age=" << cat4.GetAge() << endl; cout << "There are " << Cat::GetHowMany() << " cats alive!" << endl; Cat cat5(5); cout << "Cat5age=" << cat5.GetAge() << endl; cout << "There are " << Cat::GetHowMany() << " cats alive!" << endl; cout << "There are " <<4<< " cats alive!" << endl; cout << "There are " <<3<< " cats alive!" << endl; cout << "There are " <<2<< " cats alive!" << endl; cout << "There are " <<1<< " cats alive!" << endl; cout << "There are " <<0<< " cats alive!" << endl; return 0; }