Updated June 30, 2023
Ā
Ā
Introduction to C++ Static
C++ is a language that provides programmers the ability to have extensive control over the system resources and memory. It is generally used to develop high-performance apps. Static is a method in C++ to create variables, objects, and functions to have a specifically allocated space for the complete lifetime of a program. Once used, the static keyword is applied to variables, functions, or data members in C++. It prevents them from being modified repeatedly. Initialization of static members occurs only once, typically during the first execution of the program.
The static variables are allowed to be defined inside the function or outside of it. Zero is the default value of the static variables. In this article, different examples are discussed for the different methods of using static in C++. The methods described below for using static in C++ explain how static works using the different methods and how they can be used for different purposes.
Syntax of C++ Static
Syntax of Static Variable
static <datatype> <name_of_variable> = itās_value; // Static variable
Syntax of Static Function
static <return_type> <name_of_function> { // Static functions syntax
...
}
Working of C++ Static with Examples
Lets us discuss examples of C++ Static.
Example #1 –Ā Static Variable
Static Variable in a Function
A static variable is a kind of variable that has a space allocated throughout the life of the program. Once a static variable has been declared, it occupies a space allocated to it for the whole program. Even one can call the function numerous times, but space is allocated only once to the static variable, and the variable’s value, which was updated in the last call, is carried to the next call. Static variable helps implement co-routines in C++ in which the last state of the function has to be stored.
In the example below, a static variable āaddā has been defined, and it gets updated every time the function demo() is called. This is a basic example of a static variable in a function. The previous value of the static variable is carried forward in the next call, and the variable count is not initialized for every function call.
Code:
//Static Variable in a function
#include <iostream>
#include <string>
using namespace std;
void demo()
{
// static variable is defined
static int add = 0;
cout << add << "/";
//update in the value
//it runs till the next function is called.
add++;
}
int main()
{
for (int i=10; i>0; i--)
demo();
return 0;
}
Output:
Static Variable in the Class
The variables declared as static are initialized only for a single time, and the space allocated to them is in separate static storage. This makes the static variables get shared by the different objects. Multiple copies of a single static variable cannot be created for the varied objects. This also results in the non-initialization of the static variables with the use of constructors.
The example below shows that a static variable ājā has been created and is explicitly initialized. A scope resolution operator has been used outside the class.
Code:
//Static Variable in a class
#include<iostream>
using namespace std;
class EDUcba
{
public:
static int j;
EDUcba()
{
// Nothing is done here
};
};
int EDUcba::j = 5;
int main()
{
EDUcba pipe;
// value of j is printed
cout << pipe.j;
int p= pipe.j - 6;
cout << endl;
cout << p;
}
Output:
Example #2 – Static Members Of Class
Static Objects of Class
Objects can also be declared static, similar to how variables are declared in the previous examples. When an object is declared static, it has a scope that lasts for the entire duration of the program. In the given example, the object ‘nex’ is created as a static object within the if block. If the object had been created as a non-static object, its scope would have been limited to the if block. Once the control of the if block is exited, the destructor of the non-static object would have been invoked. To avoid this issue and ensure that the object’s destructor is invoked only after the main function ends, it is declared a static object in the program. It is only possible because of the static object and its scope throughout the programās lifetime.
Code:
// Static Class in Object.
#include<iostream>
using namespace std;
class EDUcba
{
int m = 0;
public:
EDUcba()
{
m = 0;
cout << "We Offer Trainings on:\n";
}
~EDUcba()
{
cout << "Data Science\n";
}
};
int main()
{
int o = 0;
if (o==0)
{
static EDUcba nex;
}
cout << "Machine Learning\n";
}
Output:
Static Function in a Class
Static member functions never depend on the classās object, as it was in the case of static variables and static data members in the class.Ā A static member function can be invoked using either the ‘.’ operator and an object or the scope resolution operator and the class name. However, invoking static members using the scope resolution operator and the class name is generally recommended for clarity and to avoid confusion.
Static member functions can only access static data members; they cannot access non-static member functions or non-static members of a class.
Code:
//Static function in a class
#include<iostream>
using namespace std;
class EDUcba
{
public:
// static function
static void printtext()
{
cout<<"Heyoo! Welcome to EDUcba";
}
};
// important function
int main()
{
//static function is invoked
EDUcba::printtext();
}
Output:
Conclusion
On the basis of the above article, we can understand the concept of static in C++. The different methods of using static are discussed in this article with examples and their working. The examples will help in understanding the concept and using it according to the programmerās requirements.
Recommended Articles
This is a guide to C++ Static. Here we also discuss the introduction and Working of C++ Static with its Examples and code implementation. You may also have a look at the following articles to learn more –