Static Data Member (Class variable):

  • Static Data member has the following properties:
    1. It is initialized by zero when first object of class is created.
    2. Only one copy of static data member is created for the entire class and all object share the same copy.
    3. Its scope is within class but its lifetime is entire program.
    4. They are used to store the value that are common for all the objects.
  • Static variable can be declared using the following syntax:
static  data-type    variable-name;
  • The above syntax only declare the static variable. We can initialize static variable by using following syntax:
data-type class-name :: variable-name = initial-value ;
  • Along with the definition we can also initialize the static variable.
  • If static data members (variables) are declared under the public section than it can be accessed from outside the class and if it is declared in private section than it can only be accessed within the class itself.
  • Static variables are also known as class variables because they are not the variables of any particular class.
  • Let’s consider the program given below to understand the static variable in which, we are creating static variable ‘counter’ to count the number of objects created for the class ‘MyClass’.
/* Program to demonstrate the use of static data member */

#include "iostream.h"
#include "conio.h"

class MyClass
{
        int simple;             // Simple variable
        static int counter;      // Static data member or variable

    public :
        void inc( ) ;    // to increment value of ‘counter’
        void print( ) ;    // print value of ‘counter’
};

int MyClass :: count = 0;    // Definition of static variable

void MyClass :: inc()
{
    counter++ ;
    simple++ ;
}

void MyClass :: print()
{
    count<< "Counter is : " << counter ;
}

void main( )
{
    MyClass a1;        // count = 0, simple= garbage
    a1.inc( );        // count = 1, simple = garbage + 1
    a1.print( ) ;

    MyClass a2;     // no new counter, new simple, simple = new garbage
    a2.inc( );      // count  = 2, simple = new garbage + 1
    a2.print( );
    getch() ;
}

Static Member Function:

  • Static member function has following properties:
    1. A static function can be access only by other static data member (variables) and function declared in the class.
    2. A static function is called using class name instead of object name.
  • Consider the following program (Given in above section).
/* Program to demonstrate the use of static data member & member function */

#include "iostream.h"
#include "conio.h"

class MyClass
{
        int simple;             // Simple variable
        static int counter;      // Static data member or variable

    public :
        void inc( )    //  to increment value of ‘counter’
        {
            counter++ ;
            simple++ ;     // Error ! cannot access non-static member   
        } 
        void print( ) ;    // print value of ‘counter’
};

int MyClass :: count = 0;    // Definition of static variable

void MyClass :: print()
{
    count<< "Counter is : " << counter ;
}

void main( )
{
    MyClass a1;        // count = 0, simple= garbage
    MyClass :: inc( );   // called by class name instead of an object.
    a1.print( ) ;

    MyClass a2;     // no new counter, new simple, simple = new garbage
    MyClass :: inc( );      // count  = 2, simple = new garbage + 1
    a2.print( );
    getch() ;
}