In this c++ tutorial, you will learn the following
- Static variable in c++
- Local variable in c++
- Static member variable in c++
- Combining instance and static variables
- Difference between local and static variable.
STATIC VARIABLE IN C++
- If a variable is defined with the static keyword, that is called as static variable
- Unlike local variable, it is initialized only once.
- It is stored on the static storage area, not stack memory
- Default value is zero
- It has two types. They are: local static variable and global static variable
- Local Static Variable
- Global Static Variable
Local Static Variable
- If a variable is defined inside of a function with static modifier, that is called as local static variable
- Lifetime: till end of same function
Global Static Variable
- If a variable is defined outside of a function with static modifier, that is called as global static variable
- Lifetime: till end of the program.
I. STATIC VARIABLE EXAMPLE
(LOCAL STATIC VARIABLE)
1. SOURCE CODE
#include<iostream>
using namespace std;
void disp()
{
static int k=1; // static variable definition
cout<<k<<” “;
k++; // static variable is initialized only once
}
int main()
{
cout<<“———————————–\n”;
cout<<“\tStatic Variable\n”;
cout<<“———————————–\n”;
for(int i=0;i<5;i++)
{
disp();
}
return 0;
}
2. OUTPUT
II. LOCAL VARIABLE
1. SOURCE CODE
#include<iostream>
using namespace std;
void disp()
{
int k=1; // local variable definition
cout<<k<<” “;
k++; // local variable is initialized every time
}
int main()
{
cout<<“———————————–\n”;
cout<<“\tStatic Variable\n”;
cout<<“———————————–\n”;
for(int i=0;i<5;i++)
{
disp();
}
return 0;
}
2. OUTPUT
DIFFERENCE BETWEEN LOCAL AND STATIC VARIABLE
S.N | LOCAL VARIABLE | STATIC VARIABLE |
1. | It is created without using static modifier | It is created with help of static modifier |
2. | It is initialized many times | It is initialized only once |
3. | It is stored on the stack memory | It is stored on the static storage area |
4. | It must be defined inside a function | It can be defined inside or outside of a function |
5. | Default value: Garbage value | Default value: 0 |
STATIC MEMBER VARIABLE IN C++
- If a variable is defined inside the class with the static keyword, that is called as static member variable
- Only one copy of the static variable is shared by all the objects of a class. Because static member variable purely depends on class
- Unlike instance variable, it does support direct initialization.
Accessing Static Member Variable (Calling)
- To access the static variable, there is no need to create the object of the class.
- The static member variable and static functions are accessed by using class name with scope resolution operator (::)
- Scope: till end of the class.
III. STATIC MEMBER VARIABLE
1. SOURCE CODE
#include<iostream>
using namespace std; // global namespace
class Test
{
public:
static int a; // static variable declaration
static void disp() // static function
{
cout<<“a=”<<a<<endl;
}
};
int Test::a=173; // static variable initialization using scope
resolution operator
int main()
{
cout<<“—————————————-\n”;
cout<<“\t Static Member Variable\n”;
cout<<“—————————————-\n”;
Test::disp(); // static method is called using class name with scope resolution operator
return 0;
}
2. OUTPUT
COMBINATION OF INSTANCE AND STATIC VARIABLES
- If a variable is declared with standard modifiers such as private | public | protected (without static modifier) that is called as instance variable
- If a variable is declared with static modifier that is called as static variable or static member variable
- Separate copy of an instance variable is allocated to all the objects of a class. But only one copy (common copy) of the static variable is shared between all the objects.
- If any changes in the instance variable of a particular object will not affect the values of instance variables of other objects. But any changes in the static variable must reflect the values of static variables of all the objects (entire class).
- In the picture above, you clearly understand that, separate copy of the instance variable is shared to each object of the class.
- Only a common copy (recently updated value) of a static variable is shared to all the objects of a class.
IV. COMBINING INSTANCE AND STATIC VARIABLE
1. SOURCE CODE
#include<iostream>
using namespace std;
class Test
{
public:
int k; // instance variable declaration
static int m; // static variable declaration
Test()
{
init(); // instance variable definition
k=k+50; // instance variable modification
m=m+100; // static variable modification
}
void init()
{
k=30;
}
void disp()
{
cout<<“k= “<<k<<“\n”;
cout<<“m= “<<m<<“\n”;
}
};
int Test::m=10; // static variable definition
int main()
{
cout<<“———————————–\n”;
cout<<“\t Instance vs Static Variables\n”;
cout<<“———————————–\n”;
Test t1,t2,t3;
cout<<“\t\tObject 1\n”;
t1.disp();
cout<<“———————————–\n”;
cout<<“\t\tObject 2\n”;
t2.disp();
cout<<“———————————–\n”;
cout<<“\t\tObject 3\n”;
t3.disp();
cout<<“———————————–\n”;
cout<<“Accessing static variable using objects: “<<t1.m<<” “<<t2.m<<” “<<t3.m<<“\n”;
cout<<“Accessing static variable using class: “<<Test::m<<“\n”;
cout<<“———————————–\n”;
return 0;
}
2. OUTPUT
DIFFERENCE BETWEEN LOCAL AND STATIC VARIABLE
S.N | INSTANCE VARIABLE | STATIC VARIABLE |
1. | It is created without using static modifier | It is created with help of static modifier |
2. | It supports direct initializationEx.class Test{ public: int a=25; // legal}; | It does not support direct initializationEx.class Test{ public: static int a=25; // illegal}; |
3. | It is stored on the stack memory | It is stored on the static storage area |
4. | It can be initialized inside of a class | It can be initialized outside of a class |
5. | It is accessed with help of object (It depends on object) | It is access with help of class (It depends on class) |
MORE TUTORIALS
-
How to convert Word to Number in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get convert word to number in bash script with examples.This is implemented using for loop, tr command, switch case in shell…
-
How to Count Word Frequency in Bash Script
FacebookTweetPinLinkedInEmailShares0SharePrint In this bash tutorial, you will get the program about how to count word frequency in bash script. This is done by using for loop,…
-
Bash Script – How to convert Number to Words
FacebookTweetPinLinkedInEmailShares0SharePrint In this tutorial, you will see how to convert numbers to words using bash script. This shell script can be done with the help of switch case…