A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/static-member-function-in-cpp/ below:

Static Member Function in C++

Static Member Function in C++

Last Updated : 06 Aug, 2025

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 definition.

Key points:

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:

Static member functions are important for several reasons:

For example, if you want to keep track of the number of objects created from a class, a static function can help manage that shared data.

Example:

C++
// C++ Program to show the working of
// static member functions
#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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4