Static Member Function in C++

A member function that is declared static has 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 (instead of its objects) as follows:

Class-name :: function name

//static member function
#include<iostream.h>
class test
{
int code;
static int counter;
public:
void setcode (void)
{
code=++ count;
}
void showcode (void)
{
count<<"object number:" <<code<<"\n";
}
static void showcount(void)
{
cout<<"Count:"<<Count<<"\n";
}
};
int test :: count
int main()
{
test t1, t;
t1.setcode ();
t2.setcode ();
test :: showcount (): // accessing static function
test t3;
t3.setcode ();
test :: showcount ();
t1.showcode ();
t2.showcode ();
t3.showcode ();
return 0;
}

Output

Count : 2
Count : 3
Object number : 1
Object number : 2
Object number : 3.

Read More Topics
Static data member
Overload function in C++
Nesting function in C
Error handling file I/O in C++

About the author

Santhakumar Raja

Hi, This blog is dedicated to students to stay update in the education industry. Motivates students to become better readers and writers.

View all posts

Leave a Reply