Static Data Member of Class

In C++ memory is allocated separately for each data member for different objects.
So if you change the value of data member of class using one object will not affect the value of the same data member for other object of the same class.
However sometimes it is required that all the objects of the same class share some common variablesThis can be accomplished using the concept of static data member.
We can declare static data members using static keyword.
Static data member having several characteristics which are given below:
(1) Hence all the object of the same class share static data member memory is allocated only once to the static data member. It remains common for all the objects of the same class.
(2) Static data member is initialized to 0 when first object of the class is created.


Static data member is declared in the class but it must be defined outside the class using class name and scope resolution operator (::) because memory allocation for static data member of the class is performed different then normal data member of the class and it is not the part of class object.

class item
static int count;
void DisplayCounter()
int item::count;
int main()
item a,b,c;
return 0;
Count: 1
Count: 2
Count: 3

Download Projects

Download Programs