Friday 27 March 2015

Static Data Members

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

? 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.
? It is visible only within the class, but its lifetime is the entire program.

Private Static data member

Static variables are normally used to maintain values common to the entire class. For example, a static data member can be used as a counter that records the occurrences of all the objects. First, notice the following statement in the program

int item :: count;

Note that the type and scope of each static member variable must be defined outside the class definition. This is necessary because the static data members are stored separately rather than as a par of an object. Since they are associated with the class itself rather than with any class object, they are also know as class variable .

Sample Program

#include <iostream.h>
class item
{
static int count;
int number;
public:
void getdata(int a)
{
number = a;
count++;
}
void getcount(void)
{
cout<<”count”;
cout<<count<<endl;
}
};
int item :: count;
void main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
cout<<”After reading data”;
a.getcount();
b.getcount();
c.getcount();
}

the statement int stat :: count reveals that the variable is stored separately rather than as a part of an object it is associated with the class itself rather than with any class object. It is also known as class variable.

Public Static data member

The public static data members can be accessed using the scope resolution operator or through objects with member access operator. Using the scope resolution operator is a completely new notation for member access. However, the accessibility of private static data member is same as that of normal private members.
The static data members which are declared public are similar to normal global variables. They can be addressed by the program by prefixing class name and scope resolution operator. It is illustrated in the following code fragment.

Sample Program

#include <iostream.h>

class Test
{
public:
static int pint;
private:
static int print;
};
void main()
{
Test::pint = 45; // ok
Test::print = 12; // wrong, because it private
Test Myobj;
Myobj.pint = 45; // ok
Myobj.print = 12 //wrong
}

the static data member pint defined in the class test can be accessed using the scope resolution operator prefixed by its class name as follows:

Test::pint = 45

Whereas, the data members print cannot be accessed using the scope resolution operator. Therefore, the statement

Test::print = 12; //wrong

Leads the compiler error. Objects accessing the static data member access the same data that is accessed by using the scope resolution operator.




No comments:

Post a Comment