Introduction to C++ Static
C++ is a language that provides the programmers and 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, functions to have a specifically allocated space for the complete lifetime of a program. The static keyword is used with the variables or functions or data members and once it is used they can’t be modified again and again. The initialization of the static members is done only a single time and the compiler keeps the variable till the completion 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++ explains 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 value of the variable which was updated in the last call is carried to the next call. Static variable helps in the implementation of 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 which are declared as static are initialized only for a single time and the space that are allocated to them are in separate static storage. This makes the static variables get shared by the different objects. Multiple copies cannot be created of a single static variable for the varied objects. This also results in non-initialisation of the static variables with the use of constructors.
In the example below, one can see that a static variable ‘j’ has been created and is initialized explicitly. 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
The objects can also be declared static, in the same way, the variables were declared in the above examples. When we declare the objects static, it also has the scope for the lifetime of the program. In the example below, the object ‘nex’ has been created in the if block as a static object. IN case, if the object would have been created as a non-static object then the scope of the variable would have been only inside the if block and as soon as the control of the if block would have got over, the destructor would have been invoked. This problem needs to be avoided, so the object has to be created static as it has been done in the program. This has made the destructor to get invoked once the main has ended. 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 is allowed to invoke using the ‘.’ operator and the object. Generally, it is recommended for the static members to be invoked using the scope resolution operator and the class name. Only the static data members are allowed to be accessed by the static member functions, the non-static member functions or non-static members are not allowed to be accessed.
In the example below, a static function printtext() has been created which gets invoked when the function is called using the scope resolution operator and the class name.
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 along with code implementation. You may also have a look at the following articles to learn more –
4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses