//Static Critter //Demonstrates static member variables and functions #include 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; ++s_Total; cout << "A critter has been born!" << endl; } static int GetTotal() //static member function { return s_Total; } private: int m_Hunger; }; int Critter::s_Total = 0; // initialize static member variable, ISO C++ forbids in class initialization of non constant variables 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"; system("PAUSE"); return 0; }