Static Member Function in C++

By
Last updated:

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++

Santhakumar Raja

Hello The goal of this blog is to keep students informed about developments in the field of education. encourages pupils to improve as writers and readers.

For Feedback - techactive6@gmail.com

Leave a Comment