Implementation of static keyword in C++

In this tutorial, we will learn how to implement static keyword in C++.  The keyword ‘static’ in C++ is used with variables and member functions.

Static local variables:

To declare a static variable, just write the keyword ‘static’ in front of regular variables. They are initialized with zero by default. They can retain their value till the end of the block. Hence, only one copy is created and it is available throughout the entire program. Static variables are generally used when the previous value of a variable needs to be preserved.
Let us first consider an example without using the static variable.

#include<bits/stdc++.h>
using namespace std; 
void regular()
{
  int num=0;     //non-static variable
  cout<<num++<<" ";
}
main()
{ 
  int i,j;
  cout<<"REGULAR VARIABLES : ";
  for(j=0;j<4;j++)
  {
    regular();
  }
}

OUTPUT:

REGULAR VARIABLES : 0 0 0 0

Here, we see that the space for the variable ‘num’ is allocated each time a function call is made. Hence, ‘num’ is assigned with the value 0 for every function call. Now, let us see the case of a static local variable.

#include<bits/stdc++.h>
using namespace std; 
void staticvar()
{
  static int timer;	   //static variable
  cout<<timer++<<" ";
}
main()
{ 
  int i,j;
  cout<<"STATIC VARIABLES : ";
  for(i=0;i<4;i++)
  {
    staticvar();
  }
}

OUTPUT:

STATIC VARIABLES : 0 1 2 3

Here, we see that the static variable ‘timer’ retains its value till the end of the program. Space is allocated to it only once no matter how many function calls are made. With every function call, the previous value of the variable is carried further.

Static member variables:

We can also use static keyword with class member variables. They are declared inside the class body. Only one copy of static member variables is created for the whole class, no matter how many objects are created. It has to be defined explicitly outside the class using a class name and a scope resolution operator. Static member variables can be invoked with or without an object. Let us understand this better with an example.

#include<bits/stdc++.h>
using namespace std; 
class Amount
{
  public:
    int bal=100;             //instance member variable
    static int rate;    //static member variable declaration
};
int Amount::rate=2;         //static member variable definition
main()
{
  Amount a;
  cout<<a.bal<<"\n";
  cout<<a.rate<<"\n";
  cout<<Amount::rate<<"\n";
  
}

OUTPUT:

100
2
2

Static Member Functions:

Like the static member variables, static member functions are independent of the object of the class. They can be invoked with or without the object. But, static member functions can only access static members of a class.
Let us see an example.

#include<bits/stdc++.h>
using namespace std; 
class Xam
{
  private:
    int hat;             //instance member variable
    static int bat;    //static member variable declaration
  public:
    void nonstat()
    {
      hat = 2;
      bat = 3;
      cout<<hat<<"\n"<<bat<<"\n";
    }
    static void stat()			//static member function
    {
      
      bat = 5;
      cout<<bat<<"\n";
    }	
};
int Xam::bat;        //static member variable definition
main()
{
  Xam x;
  x.nonstat();
  x.stat();
  Xam::stat();
  
}

OUTPUT:

2
3
5
5

This is how we can implement static keyword in C++. Hope you enjoyed the tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *