In this article static methods we give the information about we can invoke a that member function using object and dot operator (.) but we should use class name and scope resolution operator :: to invoke static members.
Static Member Function in C++:
We can access any Static Data Member by any Static Member Function in C++. But to access a that Data variable, a special type of Function should be used. This special type of Member Function is called Static Member Function.
This function does not apply to any object, but like static variables, this function is applied to the whole class.
To create a static member function, we have to use the static keyword while declaring the function.
Whenever a member function is declared as static, it does not depend on the objects of the class.
We can invoke a that member function using object and dot operator (.) but we should use class name and scope resolution operator :: to invoke static members.
static member functions can access only static data members and other static member functions. They cannot access non-static data members and member functions of the class.
static int stafunc() // function definition
// can access only static member data
data::stafunc(); // function call
Program based on Static Member Function in C++:
// Static member Function
int code; // auto data member
static int count; // static data member
void setcode() // member function
void showcode() // member function
cout<<“\n\n Object Number: “<<code;
static void showcount() // static member function
cout<<“\n\n Count Value: ” <<count;
int test::count; // define static data member
test p,q; // create 2 objects
p.setcode(); // function call
test::showcount(); /* accessing static function using class name and scope resolution operator*/
Count Value: 2
Count Value: 3
The Object Number: 1
Object Number: 2
Object Number: 3