Static Object In C++ - C++ Programming Tutorial
C++ Course / Object-Oriented Programming / Static Object In C++

Static Object In C++

BLUF: Mastering Static Object In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Static Object In C++

C++ is renowned for its efficiency. Learn how Static Object In C++ enables low-level control and high-performance computing in the tutorial below.

C++ represents a programming language that empowers programmers to manage a system's memory and resources. By leveraging C++ for development, we can create applications that deliver exceptional performance. Within C++, the static keyword enables the creation of variables, objects, and functions with allocated space for the entire program's duration. When static is applied to a method or variable, any subsequent modifications become unattainable.

In C++, the static member function is initialized just once throughout the entire program. The compiler retains all variables until the program finishes execution. C++ enables programmers to define static objects either within or outside functions. This tutorial will explore various forms of static in C++ along with illustrative examples.

Syntax of C++ Static

The syntax for the static variable is as below.

Example

static <datatype> <name_of_variable> = it's_value;

The syntax for the static function is as below.

Example

static <return_type> <name_of_function> { 
...
}

Static Variable in a Function

A static variable is a form of variable that reserves memory within a function throughout the duration of the program. In C++, after allocating memory to a static variable, it retains that memory for the entirety of the program's execution.

Even when the static variable is referenced multiple times, the memory allocated for it remains fixed. The static function retrieves the updated value of the variable from the previous call and uses it for subsequent calls.

In C++, the static keyword is employed to create a co-routine to retain the previous function state. In this specific instance, 'add' is designated as a static variable, which is modified upon invoking the 'demo' function.

Example 1

Example

#include <iostream>
#include <string>
using namespace std;
void demo()
{
 
static int add = 0;
cout << add << "/";
add++;
}
int main()
{
for (int i=10; i>0; i--)
demo();
return 0;
}

Output:

Static Variable in the Class

A static variable is set up just once within the program, reserving a specific memory space for it. Consequently, this static variable is accessible and modifiable by all instances. Duplicating a single static variable is not feasible. In the following illustration, the static variable 'j' is explicitly accessed and manipulated.

Example 2

Example

#include<iostream>
using namespace std;
class demo
{
public:
static int j;
demo()
{
};
};
int demo::j = 5;
int main()
{
demo pipe;
cout << pipe.j;
int p= pipe.j - 6;
cout << endl;
cout << p;
}

Output:

Static Objects of Class

Objects can also be defined as static, similar to the variables demonstrated earlier. By declaring objects as static, they maintain scope throughout the program's execution. In this specific instance, the object 'nex' is instantiated as a static object within the if block.

If the object had been instantiated as a non-static object, its variable scope would have been limited to the if block, leading to the destructor being called as soon as the block's control flow ended. To prevent this issue, the object was instantiated as static in the program. This choice ensures that the destructor is only invoked once the main function has completed execution. Such behavior is achievable due to the static object's continuous scope throughout the program's runtime.

Example 3

Example

#include<iostream>
using namespace std;
class main
{
int m = 0;
public:
main()
{
m = 0;
cout << "javacpptutorial\n";
}
~demo()
{
cout << "Data Science\n";
}
};
int main()
{
int o = 0;
if (o==0)
{
static demo nex;
}
cout << "Machine Learning\n";
}

Output:

Conclusion

Based on the preceding text, readers can grasp the concept of static in C++. This article delves into various static methods, providing examples to illustrate their functionality. These examples serve to enhance comprehension and guide programmers in utilizing the concept effectively based on their specific needs.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience