static functions and ID numbers for objects : static « Class « C++






static functions and ID numbers for objects

  
#include <iostream>  
using namespace std;  
class gamma  {  
   private:  
   static int total;        
      int id;                  
   public:  
      gamma(){  
         total++;              
         id = total;           
      }  
      ~gamma(){  
         total--;  
         cout << "Destroying ID number " << id  << endl;  
      }  
      static void showtotal(){  
         cout << "Total is " << total << endl;  
      }  
      void showid(){  
         cout << "ID number is " << id << endl;  
      }  
};  
int gamma::total = 0;
int main(){  
   gamma g1;  
   gamma::showtotal();  
  
   gamma g2, g3;  
   gamma::showtotal();  
  
   g1.showid();  
   g2.showid();  
   g3.showid();  
   return 0;  
}
  
    
  








Related examples in the same category

1.Static member variables and functions
2.Using a static data member in a class
3.Static member data.
4.Accessing static members without an object.
5.Accessing static members using non-static member functions.
6.Static member functions.
7.static members in classes
8.Calculate salary using static members.
9.static counter
10.Update static field in member method
11.Reference static method along with class name
12.static field is shared among instances