How to Declare a Static Member Function in a Class in C++?
Last Updated :
27 Feb, 2024
In C++, static functions are functions that are directly associated with a class so we can access the static function directly without creating an object of the class using the scope resolution operator. In this article, we will learn how we can declare a static function in a class in C++.
Declare a Static Function in a Class in C++
To declare a static member function, we can simply use the static keyword during the declaration of the function inside the class. Then we can use the scope resolution (::) operator to call the static function using the following syntax.
Syntax to Declare a Static Member Function
ClassName:: static_Function().
C++ Program to Declare a Static Function in a Class
C++
#include <iostream>
using namespace std;
class Student {
private :
int marks;
int id;
string Name;
public :
static void staticFunc() {
cout<< " Static function executed successfully" <<endl;
}
};
int main() {
Student::staticFunc();
return 0;
}
|
Output
Static function executed successfully
Share your thoughts in the comments
Please Login to comment...