STATIC DATA MEMBERS
1) A data member of a class can be qualified as static. A static member variable has certain special characteristics these are.
i) It is initialized to zero when the first object of its class is crated. No other initialization is permitted.
ii) 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.
iii) It is visible only within the class, but its lifetime is the entire program.
2) Static variables are normally used to maintain values common to the entire class for Ex - A static data member can be used as a counter that records the occurrences of all the objects.
STATIC MEMBER FUNCTION
1) A static member function has following properties.
i) A static function can have access to only other static members declared in the same class.
ii) A static member function can be called using the class name as follows:
classñname:: functionñname;
2) Following program illustrates the implementation of these characteristics.
class test
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout <<îobject No ì << code;
}
static void showcount(void)
{
cout<< ìcount : ì<<count;
}
}
int test:: count;
main()
{
test t1, t2;
t1.setcode();
t2.setcode();
test:::showcount();
test t3;
t3.setcode();
test:::showcount();
t1.showcode() ;
t2.showcode();
t3.showcode();
}
Output of the program
count : 2
count : 3
object No : 1
object No : 2
object No : 3
The static function showcount displays the number of objects created till that moment. A count of number of objects created is maintained by the static variable count
ALSO SEARCH:
"static data member and static member function"
"discuss the static data member and static member function in detail"
"explain static data member and static member function with example"
"difference between static data member and static member function"
"static data member and static member function program in C++"
"difference between static data member and static member function in C++"
"demonstrate static member function with static data member in one class"
"explain static data member and static member function with example in C++"
"static data member vs static member function in C++"
"we can use the static member functions and static data member"
"what do you mean by static data member and static member functions explain it with example"