A static member function in C++ is a function that belongs to the class rather than any specific object of the class. It is declared using the static keyword inside the class and can be called without creating an object.
- Can be invoked using the class name without creating an object.
- Can access only static data members and other static member functions.
- Shared among all objects of the class.
- Commonly used for utility methods and managing class-level data.
#include <iostream>
using namespace std;
class Student{
public:
static void showMessage() {
cout << "Welcome to C++ Programming";
}
};
int main() {
Student::showMessage();
return 0;
}
Output
Welcome to C++ Programming
Explanation
- showMessage() is declared as a static member function.
- It belongs to the Student class rather than any object.
- The function is called directly using the class name:
Student::showMessage();
- No object creation is required.
Declaring a static member function
class GFG {
public:
static void display(); // Declaration
};
Defining Static Data Member Functions : Define it outside the class
void GFG::display() {
// function body}
Note: Just like static data members, static functions exist independently of any object instance.
Use Cases of Static Member Functions
1. Managing Static Data Members
Static functions are commonly used to access and modify static variables.
static int count;
2. Utility or Helper Functions
Functions that do not depend on object data can be declared static.
class MathUtil {
public:
static int square(int n) {
return n * n;
}
};
3. Object Counter
Used to track the total number of objects created.
class Employee {
private:
static int count;
public:
Employee() {
count++;
}
static int getCount() {
return count;
}
};
4. Singleton Design Pattern
Static functions are used to provide a global access point to a single object instance.
Example: Accessing Static Data Members
#include <iostream>
using namespace std;
class Box
{
private:
static int length;
static int breadth;
static int height;
public:
static void print()
{
cout << "The value of the length is: " << length << endl;
cout << "The value of the breadth is: " << breadth << endl;
cout << "The value of the height is: " << height << endl;
}
};
// initialize the static data members
int Box :: length = 10;
int Box :: breadth = 20;
int Box :: height = 30;
// Driver Code
int main()
{
Box b;
cout << "Static member function is called through Object name: \n" << endl;
b.print();
cout << "\nStatic member function is called through Class name: \n" << endl;
Box::print();
return 0;
}
Output
Static member function is called through Object name: The value of the length is: 10 The value of the breadth is: 20 The value of the height is: 30 Static member function is called through Class na...
Explanation
- length, breadth, and height are static data members shared by all objects.
- print() is a static member function.
- The function accesses only static data members.
- It is called directly using:
Box::print();
- No object creation is required.
Advantages of Static Member Functions
- Can be called without creating objects.
- Saves memory because only one copy exists.
- Useful for class-level operations.
- Provides controlled access to static data members.
- Ideal for utility methods, counters, Singleton, and Factory patterns.