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

📄 static_critter.cpp

📁 the source of dev_c++ most of them for game development
💻 CPP
字号:
//Static Critter
//Demonstrates static member variables and functions

#include <iostream>

using namespace std;

class Critter
{
public:
    static int s_Total;  //static member variable
                         //total number of Critter objects in existence
    
    Critter(int hunger = 0): m_Hunger(hunger)
    {
        cout << "A critter has been born!" << endl;
        ++s_Total;
    }

    static int GetTotal()  //static member function
    {
        return s_Total;
    }
    
private:
    int m_Hunger;
};

int Critter::s_Total = 0;  // initialize static member variable

int main()
{
    cout << "The total number of critters is: ";
    cout << Critter::s_Total << "\n\n";
     
    Critter crit1, crit2, crit3;
      
    cout << "\nThe total number of critters is: ";
    cout << Critter::GetTotal() << "\n";

    return 0;
}

⌨️ 快捷键说明

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