GregLeck

Untitled

Feb 17th, 2022
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. void AddToCount()
  6. {
  7.     static int count = 0;
  8.     count++;
  9.     cout << count << endl;
  10. }
  11.  
  12. class Item
  13. {
  14. public:
  15.     Item()
  16.     {
  17.         cout << "An Item has been created!\n";
  18.     }
  19.     ~Item()
  20.     {
  21.         cout << "An Item has been destroyed!\n";
  22.     }
  23. protected:
  24.    
  25. private:
  26. };
  27.  
  28. class Critter
  29. {
  30. public:
  31.     // Must be initialized outside the class
  32.     static int CritterCount;
  33.  
  34.     static void AnnounceCount()
  35.     {
  36.         cout << CritterCount << endl;
  37.     }
  38. };
  39. int Critter::CritterCount = 0;
  40.  
  41.  
  42. int main()
  43. {
  44.     // Will print '1' and '2'
  45.     AddToCount();
  46.     AddToCount();
  47.  
  48.     {
  49.         // Item is created and destroyed because of scope
  50.         // unless Item is declared 'static'
  51.         static Item item;
  52.     }
  53.  
  54.     // 'CritterCount' is accessible even though
  55.     // no Critter object was instantiated
  56.     // If Critter object were instantiated, 'CritterCount' value would be shared
  57.     // among all Critter objects.
  58.     Critter::CritterCount = 13;
  59.     cout << Critter::CritterCount << endl;
  60.     Critter::AnnounceCount();
  61.  
  62.  
  63.     system("pause");
  64. }
Advertisement
Add Comment
Please, Sign In to add comment