Skip to main content

Static Data Member

 A data member of a class can be qualified as statics. The properties of a static member variable are similar to that of a C static variable. A static member variable has certain special characteristics. These are:

  • It is initialized to zero when the first object of its class is created. No other initialization is permitted.
  • Only one copy of that member is created for the entire class and is shared by all the objectives of that class, no matter how many objects are created.
  • It is visible only within the class, but its lifetime is the entire program.

#include <iostream>

using namespace std;

class item
{
       static int count;
       int number;
       public:
       void getdata(int a)
       {
           number=a;
           count ++;
       }
       void getcount(void)
       {
           cout<< "count";
           cout<< count <<"\n";
       }
};
int item::count;
int main()
{
        item a,b,c;
        a.getcount();
        b.getcount();
        c.getcount();
        
        a.getdata(100);
        b.getdata(200);
        c.getdata(300);
        
        cout<<"After reading data" << "\n";
        
        a.getcount();
        b.getcount();
        c.getcount();
        return 0;
}


Comments