In this c++ tutorial, you will learn how to use static functions in c++ with examples.
STATIC FUNCTION (STATIC MEMBER FUNCTIONS)
- If a function is defined with the static keyword, that is called as static function
- It is used to access only static member variable and other static functions
- It purely depends on class
- It is called by using class-name with scope resolution operator
- Like a static data member, the static member function is also a class function. It is not associated with any class object
- Static function work for the class as whole rather than for a particular object of a class
- It is not possible to access Instance variable (Non static variable) through static function.
Accessing Static function
- Static members are purely depending on class
- Static function can be called using class name with scope resolution operator
- Also, it is possible to access static functions using object with dot (.) operator.
STATIC MEMBER FUNCTION
1. SOURCE CODE
#include<iostream>
using namespace std;
class Test
{
public:
static void disp() // static function
{
cout<<“Hello World\n”;
}
};
int main()
{
cout<<“———————————–\n”;
cout<<“\t Static Function\n”;
cout<<“———————————–\n”;
Test::disp(); // calling static function using class-name
return 0;
}
2. OUTPUT