Example 1
Let’s check out this first instance of a static variable in C++.
#include <string>
using namespace std;
void My_Demo()
{
static int count_val = 0;
cout<<count_val<< " ";
count_val++;
}
int main()
{
for (int i=0; i<4; i++)
My_Demo();
return 0;
}
Within the function “My_Demo”, we have created the static variable “count_val” by utilizing the keyword static and setting it with the value zero. After that, the count_val is incremented for the next function call. Then, we have the main function where we have a for loop for iteration and invoke the function “My_Demo”.
The outcome of the above static variable script is as follows:
Example 2
The static variables in a class are accessible by the objects since they are assigned space in separate static storage, which means that variables marked as static are initialized only once. The same static variables cannot be used more than once for different objects. Additionally, due to this, constructors cannot initialize static variables. As a result, the user needs manually initialize static variables inside of classes by using the class name and the scope resolution operator beyond the class, as illustrated below:
using namespace std;
class MyClass
{
public:
static int x;
MyClass()
{
//empty body
};
};
int MyClass::x = 2;
int main()
{
MyClassobj;
cout<<obj.x;
}
We have established a class, “MyClass,” which is kept public. Then, we have defined a static variable “x”. Then, we created the constructor of the class “MyClass,” which does nothing because of its empty body. Outside the class, we have explicitly initialized the static variable “x” with the scope resolution “::”. Then, inside the int main, we have declared the object “obj” by invoking the “MyClass” and printing its value.
The value of the static variable x set in the above script is displayed below.
Example 3
As you can see from the program above, we made an effort to make several duplicates of the static variable for various objects. But this did not take place.
using namespace std;
class MyClass2
{
int a;
public:
MyClass2()
{
a = 0;
cout<< " Constructor Body\n";
}
~MyClass2()
{
cout<< "Destructor Body\n";
}
};
int main()
{
int b = 0;
if (b==0)
{
MyClass2 obj;
}
cout<< "Main Terminated\n";
}
We have established a class, “MyClass2,” and declared an object as “a”. Then, we have defined the constructor of “MyClass2,” which initializes the object “a” with the zero value. Also, it will cout the statement “Constructor Body”. Moreover, we have constructed the destructor of the class “MyClass2,” which only prints the statement “Destructor Body”. After that, we have created the int main function, which defines and initializes another object as “b” set as zero. Also, we have an if statement which has the condition that the object “b” is equal to the value zero and then defines the constructor MyClass2 object as “obj”. In the end, we have called the cout command, which will print the message of the main terminated.
The following outcome is printed on the prompt.
Example 4
The object is designated as non-static in the aforementioned program’s if block. Therefore, the scope of the variable is restricted to the if block. As a result, the function constructor is called when an object is created, and the destructor is called as soon as the if block’s control is no longer in effect because an object’s scope is limited to the if block, where it is declared. Now let’s examine how the output will change if we declare the object to be static.
using namespace std;
class MyClass3
{
int x = 0;
public:
MyClass3()
{
x = 0;
cout<< "Constructor Section\n";
}
~MyClass3()
{
cout<< "Destructor Section\n";
}
};
int main()
{
int y = 0;
if (y==0)
{
static MyClass3 obj;
}
cout<< "Main Ended\n";
}
We have created the class “MyClas3” like in the above program. Then, we defined the variable x and assigned it a value of zero. The constructor and destructor are also established inside the class “MyClass3”. Within the main, if conditional statement, we have declared the static object of “MyClass3”.
The change in performance is obvious. Because the destructor is called in the subsequent prompt screen after the main, this took place as a result of the program’s lifespan being covered by static objects.
Example 5
The existence of static member functions within a class is independent of any class object, just like that of static data members or static variables. In contrast to the class’s non-static data members and member functions, static member functions are only permitted access to other static member functions or other static data members.
using namespace std;
class New_Class
{
public:
static void Display_Msg()
{
cout<<"Welcome to my portal";
}
};
int main()
{
New_Class::Display_Msg();
}
The class “New_Class” has a public modifier that defines the static void member function as “Display_Msg”. Inside that static “Display_Msg” member function, we have printed the cout statement. Outside the “New_Class,” we have int main method of the program where we have invoked the static member function “Display_Msg”.
The output prints the message inside the static member function “Display_Msg” as follows:
Conclusion
Our understanding of the static idea in C++ can be based on the aforementioned article. In the static storage area, static items are only ever allotted storage once during a program. In addition, they are valid for the whole period of the program. This guide discusses the various static usage techniques along with working examples. Using the concepts according to the programmer’s needs will be easier with the aid of the examples.