⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 list1501.cpp

📁 teach yourself C++ in 21 days 第五版
💻 CPP
字号:
//Listing 15.1 static data members

#include <iostream>
using namespace std;

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!" << endl;
      cout << "Deleting the one that is ";
      cout << CatHouse[i]->GetAge();
      cout << " years old" << endl;
      delete CatHouse[i];
      CatHouse[i] = 0;
   }
   return 0;
}

⌨️ 快捷键说明

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