Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- using namespace std;
- void AddToCount()
- {
- static int count = 0;
- count++;
- cout << count << endl;
- }
- class Item
- {
- public:
- Item()
- {
- cout << "An Item has been created!\n";
- }
- ~Item()
- {
- cout << "An Item has been destroyed!\n";
- }
- protected:
- private:
- };
- class Critter
- {
- public:
- // Must be initialized outside the class
- static int CritterCount;
- static void AnnounceCount()
- {
- cout << CritterCount << endl;
- }
- };
- int Critter::CritterCount = 0;
- int main()
- {
- // Will print '1' and '2'
- AddToCount();
- AddToCount();
- {
- // Item is created and destroyed because of scope
- // unless Item is declared 'static'
- static Item item;
- }
- // 'CritterCount' is accessible even though
- // no Critter object was instantiated
- // If Critter object were instantiated, 'CritterCount' value would be shared
- // among all Critter objects.
- Critter::CritterCount = 13;
- cout << Critter::CritterCount << endl;
- Critter::AnnounceCount();
- system("pause");
- }
Advertisement
Add Comment
Please, Sign In to add comment