C++

Use of Static Member Function in C++

The block of code that is defined with a name in any programming language is called function. The code of a function executes when the function is called anywhere from the code. Two types of functions can be declared. One is a normal function, and another is a static member function. The static member function is declared for a special purpose. The static keyword is used to declare the static member function. This type of function can access other static variables or functions of the same class only. This function is called by using the name of the class where the function is declared. In this tutorial, the use of the static member function in the C++ class will be discussed.

Example 1: Use of Simple Static Member Function

The way to define a simple static member function in a C++ class and call the function using the class name is shown in the following example. A class with a static member function named Info() has defined in the code what will print a simple text message. The Info() function has been called using the class name without creating the object of the class.

//Include library for I/O
#include<>iostream>

using namespace std;

//Define the class
class UseOfStaticMember
{
    public:
    //Declare static member function
    static void Info()
    {
        cout << "The static member function is called.\n";
    }
};

int main()
{
    //Calling static member function
    UseOfStaticMember::Info();
}

Output:

The following output will appear after executing the above code:

Example 2: Print the Static Variable From the Static Member Function

The way to implement the static variable using the static member function of a class is shown in the following example. A class with a standard variable, a static variable, two public functions, and a static member function has been declared in the code. The increment() function has been defined to increment the values of both variables. The display_val() function has been defined to print the value of the normal variable. The display_static_val() function has been defined to print the value of the static variable. Two objects of the class have been created, and the increment() function has been called two times by using the two objects. Next, the display_val() function has been called using the second object, and the display_static_val() function has been called using the class name.

//Include library for I/O
#include <iostream>
using namespace std;

//Define the class
class UseOfStaticMember
{
    static int static_num;
    int normal_num = 0;
    public:
    //Declare the function to increment the values of the numbers
    void increment(){
        normal_num++;
        static_num++;
    }
    //Declare the function to display the value of normal_num
    void display_val(){
        cout << "The current value of normal number = " << normal_num << "\n";
    }
    //Declare static member function
    static void display_static_val(){
        cout << "The current value of static number = " << static_num << "\n";
    }
};

//Declare the static data member
int UseOfStaticMember:: static_num;


int main()
{
    //Create the objects of the class
    UseOfStaticMember object1, object2;
    //Increment the values using the first object
    object1.increment();
    //Print the values of the normal variable
    object1.display_val();
   
    //Increment the values using the second object
    object2.increment();
    //Print the values of the normal variable
    object2.display_val();
    //Print the values of the static variable
    UseOfStaticMember::display_static_val();
    return 0;
}

Output:

The following output will appear after executing the above code:

Example 3: Access Static Member Function Using Class and Object

In the previous two examples, the static member function of the class has been accessed using the class name. The ways to access the static member function by using both the class name and the object of the class have been shown in this tutorial. A class with three static variables and a static member function has been defined in the code. The static variables have been initialized outside the class. Next, the static member function named Display() has been called directly by using the class name and the object of the class.

//Include library for I/O
#include <iostream>
using namespace std;

//Define the class
class UseOfStaticMember
{
    private:
    //Define the static data members
    static string ID;
    static string Name;
    static int Salary;

    public:
    //Define the static member function
    static void Display()
    {
        cout <<"ID: " << ID << "\n";
        cout <<"Name: " << Name << "\n";
        cout <<"Salary: " << Salary << "\n";
    }
};

//Initialize the static data members
string UseOfStaticMember :: ID = "673456";
string UseOfStaticMember :: Name = "Mahmuda Afroz";
int UseOfStaticMember :: Salary = 70000;

int main()
{
    cout<<"Print static memebers using class name:\n";
    UseOfStaticMember::Display();

    UseOfStaticMember obj;
    cout << "\nPrint static members using object:\n";
    obj.Display();
    return 0;
}

Output:

The following output will appear after executing the above code:

Example 4: Count the Total Number of Objects

The way to count and print the total number of objects of a class is by using a static member variable and function, as shown in the following example. A class with a constructor, a static variable, and a static member function has been defined in the code. The value of the static variable is initialized to 0. The value of the static variable will be incremented by 1 when an object of the class will be created. Three objects of the class are created inside the main() function. Next, the static member function has been called to print the total number of objects of the class.

//Include library for I/O
#include  <iostream>
using namespace std;

//Define the class
class UseOfStaticMember {
    //Declare the static data member
    static int Counter;

    public:
    //Declare the Constructor
    UseOfStaticMember() {

        cout <<"The constructor is called.\n";
        //Increase the value of the static data member
        Counter++;
    }
    static int count_objects()
    {
        return Counter;

    }

};

//Initialize the static member
int UseOfStaticMember::Counter = 0;

int main(void) {
   
    //Create three objects of the class
    UseOfStaticMember object1, object2, object3;

    //Print the total number of objects created
    cout << "Total number of objects: " << UseOfStaticMember::count_objects() << "\n";

    return 0;
}

Output:

The following output will appear after executing the above code:

Conclusion:

The uses of static member function with static variables in C++ class have been explained in this tutorial using various examples. I hope using the static member function will be clear for the readers after reading this tutorial.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.