Thanks to visit codestin.com
Credit goes to www.tutorialspoint.com

Static Variables in Member Functions in C++



The static variables in C++ are defined using the static keyword. The static member variables in a class are shared by all the class objects as there is only one copy of them in memory, regardless of the number of objects of the class. All static data is initialized to zero when the first object is created, if no other initialization is present.

In this article, we will understand the static member variables, their characteristics, and how static variables work in member functions with the help of example code. The characteristics of the static keyword are mentioned below:

Characteristics of static Member Variable

Here are the characteristics of static member variables:

  • For any one class, there exists just one copy of that static member and is shared by all the objects of that class.
  • The static data member is a class member, i.e., independent of object creation. You do not need to create a class to access the static data members.
  • The static data member is declared inside a class but is defined outside the class using the scope resolution operator(::).
  • The static data members can be accessed using the class names. You can use objects too for accessing static data members, but that is not preferable, as static members are independent of object creation.
  • The static data members can have any access modifiers, i.e., either public, private, or protected.

How Static Variable Work in Member Function?

The static variable defined in the member function is initialized only once when the function is called for the first time. When we use the local variables, they get reinitialized after each function call, but the static variable maintains its value between function calls. It regains the last value for future function calls.
The static variable remains in memory until the program gets executed or completed. The static variable is not destroyed upon function completion. Here are some code examples of how normal and static variables work in a member function:

Example 1

In this example, we are counting the total number of function calls using the static variable and an int variable. The static variable retains the count of previous call for the future function calls whereas the int variable gets reset to its initial value after each function call.

#include <iostream>
using namespace std;

void statFun(int n)
{
   static int counter = 0;
   counter++;
   if (n == 0)
   {
      cout << "Total calls made with static variable: "
           << counter << "\n";
      return;
   }
   statFun(n - 1);
}

void normalFun(int n)
{
   int counterTwo = 0;
   counterTwo++;
   if (n == 0)
   {
      cout << "Total calls made without static variable: "
           << counterTwo << "\n";
      return;
   }
   normalFun(n - 1);
}

int main()
{
   statFun(5);
   normalFun(5);
   return 0;
}

The output of the above code is as follows:

Total calls made with static variable: 6
Total calls made without static variable: 1

Example 2

In this example, we are printing the value of the static and int variables after each function call. The count of the static variable increases with each call where whereas the count of the int variable remains the same.

#include <iostream>
using namespace std;
// With static 
void statFun()
{
   static int num = 1;
   cout << "Value of num: " << num << "\n";
   num++;
    
}
// Without static 
void normalFun()
{
   int number = 1;
   cout << "Value of number: " << number << "\n";
   number++;
    
}

int main()
{
   statFun();
   statFun();
   statFun();
   normalFun();
   normalFun();
   normalFun();
   return 0;
}

The output of the above code is as follows:

Value of num: 1
Value of num: 2
Value of num: 3
Value of number: 1
Value of number: 1
Value of number: 1
Updated on: 2025-05-16T17:24:40+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements