You are reading the article Guide To Working Of C++ Static With Examples updated in September 2023 on the website Lanphuongmhbrtower.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Guide To Working Of C++ Static With Examples
Introduction to C++ StaticC++ 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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax of C++ StaticSyntax of Static Variable
Syntax of Static Function
… }
Working of C++ Static with ExamplesLets us discuss examples of C++ Static.
Example #1 – Static VariableStatic 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:
using namespace std; void demo() { static int add = 0; cout << add << "/"; add++; } int main() { 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.
Code:
using namespace std; class EDUcba { public: static int j; EDUcba() { }; }; int EDUcba::j = 5; int main() { EDUcba pipe; cout << pipe.j; int p= pipe.j - 6; cout << endl; cout << p; }Output:
Example #2 – Static Members Of ClassStatic 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:
using namespace std; class EDUcba { int m = 0; public: EDUcba() { m = 0; cout << "We Offer Trainings on:n"; } ~EDUcba() { cout << "Data Sciencen"; } }; int main() { int o = 0; if (o==0) { static EDUcba nex; } cout << "Machine Learningn"; }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:
using namespace std; class EDUcba { public: static void printtext() { cout<<"Heyoo! Welcome to EDUcba"; } }; int main() { EDUcba::printtext(); }Output:
ConclusionOn 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 ArticlesThis 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 –
You're reading Guide To Working Of C++ Static With Examples
Update the detailed information about Guide To Working Of C++ Static With Examples on the Lanphuongmhbrtower.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!