Example of Accessing Static Members


/************************************************************************
*

The following example uses the class access operators to access static members.

                                                                        *
************************************************************************/


// This example illustrates access to static
// members with class access operators.

#include <iostream.h>
class X
{
      static int cnt;
public:
      // The following routines all set X's static variable cnt
      // and print its value.
      void Set_Show (int i)
      {      X::cnt = i;
         cout << "X::cnt = " << X::cnt << endl; }
      void Set_Show (int i, int j )
      {       this->cnt = i+j;
         cout << "X::cnt = " << X::cnt << endl; }
      void Set_Show (X& x, int i)
      {       x.cnt = i;
         cout << "X::cnt = " << X::cnt << endl; }
};
int X::cnt;
void main()
{
      X xobj1, xobj2;
      xobj1.Set_Show(11);
      xobj1.Set_Show(11,22);
      xobj1.Set_Show(xobj2, 44);
}


/************************************************************************
*

The above example produces the following output:

X::cnt = 11
X::cnt = 33
X::cnt = 44


                                                                        *
************************************************************************/