Static Data Members: The characteristics of static member variables 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 objects of that class, no matter how many objects are created.
static DataType DataMember;
. . . .
DataType className :: DataMember[=InitialValue];
Example: Program to count how many calls are made to a member function set()
static int count; //static data member
void set(int num) //initializes object's member and increments function call
void show( )
cout <<"\n Number of calls made
int MyClass :: count:=0; //definition and initialization of a data member
int main( )
obj 1 .show( ); //Output: Number of cal Is made to set() through any object: 0
obj 1 .show( ); //Output: Number of made to set() through any object: 1
obj2.show( ); //Output: Number of calls made to set() through any object: 2
obj3.show( ); //Output: Number of calls made to set() through any object: 3