Static Member Function of Class


Like data member of the class, member function of the class can also be declared as a static.
Static member functions are designed to work with static data members.
In order to make a member function as static we need to precede the function declaration with static keyword.
Static member function having several characteristics which are given below:
(1) Static member function can access only static member of the class in which it is declared.
(2) Static member function is not a part of class object so it can not be called using object of the class.

     

Static member function can be called using class name and scope resolution operator as shown below:
Class_Name :: Function_Name ();

Example:
#include <iostream.h>
#include <conio.h>
class test
{
private:
static int count;
public:
void setCount(void);
static void DisplayCounter(void);
};
void test :: setCount(void)
{
count++;
}
void test :: DisplayCounter(void)
{
cout<<"Count:"<< count << endl;
}
int test::count;
int main(void)
{
test t1, t2;
clrscr();
t1.setCount();
test :: DisplayCounter();
t2.setCount();
test :: DisplayCounter();
getch();
return(0);
}
Output:
Count: 1
Count: 2

Download Projects


Download Programs