DEFINITION 18: STATIC - PRATMG/2143-OOP-Tamang GitHub Wiki

There are two ways we can use static keyword:

  • Static variables
  • Static members of class

STATIC VARIABLE:

Static variable also can be used in two ways:

1. Static variables in a Function:

When a variable is declared static, space is reserved for it for the duration of the program. Even if the function is called multiple times, space for the static variable is only allocated once, and the value of the variable from the previous call is carried over to the next function call. This is useful when implementing coroutines in C/C++ or any other application where the previous state of the function must be saved.

// C++ program to demonstrate
// the use of static Static
// variables in a Function
#include <iostream>
#include <string>
using namespace std;

void demo()
{
	// static variable
	static int count = 0;
	cout << count << " ";
	
	// value is updated and
	// will be carried to next
	// function calls
	count++;
}

int main()
{
	for (int i=0; i<5; i++)	
		demo();
	return 0;
}
2. Static variables in a class:

Since static variables are only initialized once so they are allocated space in separate static storage, the static variables in a class are shared by the objects. Multiple copies of the same static variable for different objects are not permitted. As a result, static variables cannot be initialized using constructors. a static variable inside a class should be initialized explicitly by the user using the class name and scope resolution operator outside the class.

// C++ program to demonstrate static
// variables inside a class

#include<iostream>
using namespace std;

class GfG
{
public:
	static int i;
	
	GfG()
	{
		// Do nothing
	};
};

int GfG::i = 1;

int main()
{
	GfG obj;
	// prints value of i
	cout << obj.i;
}

STATIC MEMBERS OF CLASS:

Same as static variables, static members of class can also used in two ways:

1. Class objects as static:

Objects, like variables, have a scope that lasts the duration of the program when declared as static. When the object is created, the constructor is called, and as soon as the control of the if block is taken over, the destructor is called, because the scope of the object is only inside the if block where it is declared.

// CPP program to illustrate
// class objects as static
#include<iostream>
using namespace std;

class GfG
{
	int i = 0;
	
	public:
	GfG()
	{
		i = 0;
		cout << "Inside Constructor\n";
	}
	
	~GfG()
	{
		cout << "Inside Destructor\n";
	}
};

int main()
{
	int x = 0;
	if (x==0)
	{
		static GfG obj;
	}
	cout << "End of main\n";
}
2. Static functions in a class:

Static member functions, like static data members or static variables within the class, are independent of the class's object. We can call a static member function with the object and the '.' operator, but it is preferable to call static members with the class name and the scope resolution operator. Static member functions can only access static data members or other static member functions; they cannot access the class's non-static data members or member functions.

// C++ program to demonstrate static
// member function in a class
#include<iostream>
using namespace std;

class GfG
{
public:
	
	// static member function
	static void printMsg()
	{
		cout<<"Welcome to GfG!";
	}
};

// main function
int main()
{
	// invoking a static member function
	GfG::printMsg();
}

Reference:

⚠️ **GitHub.com Fallback** ⚠️