Static Members
by hkesavaraju on Jan.28, 2010, under Advanced Data Structures
Static Members:
The static members can be static data members or static member functions.
(i) static Data Members: C++ also allows data members in a can be static. A static variable in c++ has same properties of c static variable.
The properties are:
-
It is initialized to 0 when first object of its class is created.
-
Only one data member is created for the entire class and is allowed to be shared by all objects of the class.
-
Its scope is within the class only but its life time is in entire program.
A static variable maintain values common to the entire class. It is used as a counter that records the occurrences of all the objects.
The example program is:
#include<iostream.h>
#include<conio.h>
class item
{
int number;
static int count;
public:
void getdata(int a)
{
number=a;
count++;
}
void getcount()
{
cout<<”count is”<<count<<endl;
}
};
int item::count;
main()
{
clrscr();
item i1,i2,i3;
i1.getcount();
i2.getcount();
i3.getcount();
i1.getdata(100);
i2.getdata(200);
i3.getdata(300);
cout<<”after reading data”<<endl;
i1.getcount();
i2.getcount();
i3.getcount();
getch();
}
output is: count is:0
count is:0
count is:0
after reading data
count is:3
count is:3
count is:3
In above program,int item::count is defined outside of the class definition.
-
The type and scope of Static variable is defined outside of the class definition. It is necessary because these can be stored separately rather than as part of the class.
-
These are called class variables because these are associated with class itself rather than class objects.
-
A static variable can also be assigned with any initial value while defining it.
int item::count=10; // static variable count takes 10 initially
(b) static member functions: Like static data members, static member functions can also be defined. The properties of static member functions are:
(i) A static member function can only access other static members which declared in same class.
(ii) It can be called using the class name like the following form
class_name::function_name;
Example is : item::showcount();
The program that illustrates static member function is:
#include<iostream.h>
#include<conio.h>
class test
{
static int count;
int code;
public:
void setcode()
{
code=++count;
}
void showcode()
{
cout<<”object code is:”<<code;
}
static void showcount()
{
cout<<”count is:”<<count;
}
};
int test::count;
main()
{
clrscr();
test t1,t2;
t1.setcode();
t2.setcode();
test:;showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
getch();
}
the output is: count is:2
count is:3
object code is:1
object code is:2
object code is:3
In above code, the statement code=++count; is executed whenever setcode() function is invoked and current value of count is assigned to code.
When showcode() is called on each object, Each gets its own copy of code value.
Note: When non static data member is used in static function showcount that will not work.
Ex: static void showcount()
{
cout<<code; // code is not static data member
}