What is Static Member Function in C++?

Published on Sep 06,2019 116 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

Static is a keyword in C and C++ which is used to declare a special type of a variable or a function inside or outside of a class. In this post, we will briefly understand the concept of static member variables and static member functions in c++ and compare them with normal variables and functions in the following order:

 

Static Member Variables

Variables classified as static are also a part of C. suppose in a function there are 2 variables, one is a normal variable and the other one is a static variable. The normal variable is created when the function is called and its scope is limited. While the static variable is created once and destroyed at the end of the program. These variables have a lifetime throughout the program.

#include <iostream>

using namespace std;
void Test(){
    
    static int x = 1;
     x = ++x;
    
    int y = 1;
    y = ++y;
    cout<<"x = "<<x<<"n";
    cout<<"y = "<<y<<"n";
}
int main()
{  
    Test();
    Test();
        
    return 0;
}

Output:

static-member-variables-1

From the above output, we can conclude that every time the Test( ) function was called a copy of variable ‘y’ was created while the same copy of variable ‘x’ was used every time the Test( ) function was called.

Now, let’s discuss the characteristics of the static variables

  1. Static variables are initialized to 0. It is initialized only once.

  2. Throughout the program, only one copy of the static member variable is created for the entire class hence static member variables are also called class variables. It is shared by all instances of the class.

  3. The static member variable is only visible within the class but its lifetime is till the program ends.

Let’s consider an example of static member variables in a class.

#include <iostream>

using namespace std;

class Example{
    
    static int x;
    
public:
    
    void function1(){
        x++;
    }
    
    void function2(){
        cout<<"x = "<<x<<"n";
    }
    
};

int Example :: x;

int main()
{
    Example obj1, obj2, obj3;
    
    cout<<"Initial value of x" <<"n";
    
    obj1.function2();
    obj2.function2();
    obj3.function2();
    
    obj1.function1();
    obj2.function1();
    obj3.function1();
    
    cout<<"Value of x after calling function1"<<"n";
    
    obj1.function2();
    obj2.function2();
    obj3.function2();
    

    return 0;
}

Output:

static-member-variables-2

From the above output, we can see that the variable ‘x’ is shared across all the objects. To understand the concept of the static data variables in detail we can think of a library where there are several books placed on different shelves. Consider the library as a class, position of a certain book ‘x’ as a static member variable and students as the objects of the class. When the first student arrived he places ‘x’ at a new position now when another student arrives ‘x’ won’t return to its original position but it will remain where the first student left it.

 

Static Member Functions in C++

Just like static member variables we have static member functions that are used for a specific purpose. To create a static member function we need to use the static keyword while declaring the function. Since static member variables are class properties and not object properties, to access them we need to use the class name instead of the object name.

Properties of static member functions:

  1. A static function can only access other static variables or functions present in the same class

  2. Static member functions are called using the class name. Syntax- class_name::function_name( )

Let’s consider a classic example to understand the concept of static member functions in detail. In this example, we will understand all the concepts related to static member functions.

#include <iostream>

using namespace std;

class Example{
    static int Number;
    int n;
    
    public:
    
    void set_n(){
        
        n = ++Number;
    }
    
    void show_n(){
        
        cout<<"value of n = "<<n<<endl;
    }
    
    static void show_Number(){
        
        cout<<"value of Number = "<<Number<<endl;
    }
    
    
};

int Example:: Number;

int main()
{
    Example example1, example2;
    
    example1.set_n();
    example2.set_n();

    example1.show_n();
    example2.show_n();
    
    Example::show_Number();
    
    return 0;
}

Output:

static-member-function-in-c++

From the above output, we can see that the value of the variable ‘n’ is different for both the objects ‘example1’ and ‘example2’ of the class ‘Example’. Since the variable ‘Number’ is a class variable its value is the same for both the objects ‘example1’ and ‘example2’. Static member variables and functions are used when common values are to be shared across all the objects. While programming, the use of static keyword should be done wisely.

 

With this, we come to an end of this article on Static member function in c++. If you wish to learn more, check out the Java Training by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog and we will get back to you as soon as possible.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.