Static Members and Functions
Static Data Members & Functions
1. Static Data Members
A data member of class
can be qualified as static. A static member variable has certain special
characteristics which are as follows:
·
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.
Static variables are
normally used to maintain values common to the entire class.
Example:
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
int number;
public:
void getdata(int
a)
{
number = a;
count++;
}
void getcount()
{
cout<<”Count: ”;
cout<<count<<”\n”;
}
};
int
item :: count; // static member definition
int main()
{
item a,b,c; // count is initialized to zero
a.getcount(); // display count
b.getcount();
c.getcount();
a.getdata(100); // getting data into object a
b.getdata(200); // getting data into object b
c.getdata(300); // getting data into object c
cout<<”After reading data”<<”\n”;
a.getcount(); // display count
b.getcount();
c.getcount();
return 0;
}
Output
of the program
Count: 0
Count: 0
Count: 0
After reading data
Count: 3
Count: 3
Count: 3
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 part, of an object. Since they are associated
with the class itself rather than with any class objects they are also known as
class variables.
The static variable count
is initialized to zero when the objects are created. The count is incremented
whenever the data is read into an object. Since the data is read into objects
three times, the variable count is incremented three times. Because there is
only one copy of count shared by all the three objects, all the three
output statements cause the value 3 to be displayed.
int item :: count = 10; // initialize the value of count to 10
2.
Static Member Function
Like static member variable, we can also have
static member functions. It has
following properties:
·
A static
function can have access to only static members declared in this class.
·
A static
member function belongs to all the objects of the class.
·
A static
member function can be called using class name as follows:
class-name
:: function name;
Following program
illustrates the implementation of these characteristics. The static function
showcount() displays the no. of objects created that moment. A count number
of objects created is maintained by static variable count.
The function showcode()
displays the code number of each object.
Program on Static Member
Function
#include <iostream.h>
#include<conio.h>
class test
{
int code;
static int count; // static member variable
public:
void setcode()
{
code=count++;
}
void showcode()
{
cout<<” Object Number : ”<<code<<”\n”;
}
static void showcount()
{
cout<<” Count :
”<<count<<”\n”; // static member function
}
};
int
test :: count;
void main()
{
test
t1, t2 ;
t1.setcode();
t2.setcode();
test
:: showcount(); //
accessing static function
test t3;
t3.setcode();
test
:: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
}
Output
of Program
Count : 2
Count : 3
Object Number : 0
Object Number : 1
Object Number : 2
Comments
Post a Comment