Friday, 27 March 2015

Static Member Functions

Like static member variable, we can also have static member functions. A member function that is declared static has the following properties:

? A static function can have access to only other static members declared in the
  same class.
? A static member function can be called using the class name as follows:

                    Class-name :: function -name;

The general syntax for defining static member function is

Static return_type function-name(arguments)
{
function body;
}

Sample Program

#include <iostream.h>

Class test
{
int code;
static int count;
public:
void setcode(void)
{
code = ++count;
}
void showcode(void)
{
cout<<”Object number “<<code<<endl;
}
static void showcount(void)
{
cout<<”count is “<<count<<endl;
}
};

int test::count;

void main()
{
test t1,t2;
t1.setcode();
t2.setcode();
test::showcount();
test t3;
t3.setcode();
test::showcount();
t1.showcode();
t2.showcode();
t3.showcode();
}

No comments:

Post a Comment