Storage Classes in C++

Storage Classes in C++

Storage classes in C++ is used to define and describe the lifetime and visibility or features of a variable and function. This features contains the scope, visibility which help us to find the existence of a particular variable during the runtime of a program

Syntax of storage class

storage_class var_data_type var_name; 

There are five types of storage classes, they are follows

  1. Automatic
  2. Register
  3. Static
  4. External
  5. Mutable

1. Auto Storage Class in C++

Automatic storage class is the default storage class for all local variables, which are declared inside a block. Keyword auto is used for declaring automatic variables.

Syntax of auto strorage class in C++

auto datatype var_name1 [= value];

Program of auto storage class in C++

#include <iostream>
using namespace std;
 
void autoStorageClass()
{
 
    cout << "Auto class\n";
 
    auto a = 56;
    auto b = 5.6;
    auto c = "StudyExperts";
    auto d = 'S';
 
    cout << a << " \n";
    cout << b << " \n";
    cout << c << " \n";
    cout << d << " \n";
}
 
int main()
{
    autoStorageClass();
 
    return 0;
}

Output

Auto class
56 
5.6 
StudyExperts
S

2. Register Storage Class

register keyword is used for specifying register variables.

Register variables are similar to auto variables that is having same functionality and exists inside a particular function only. It is supposed to be faster than the variables stored in memory during runtime program

Program of register storage class in C++

#include <iostream>
using namespace std;
 
void registerStorageClass()
{
 
    cout << " Register class\n";
 
 
    register char b = 'G';
 
    
    cout << "Value of the variable 'b'"
         << " declared as register: " << b;
}
int main()
{
 
    registerStorageClass();
    return 0;
}

Output

Register class
Value of the variable 'b' declared as register: G

3. Static Storage Class

static keyword is used to declare a static variable.

The static variable is initialized only one time and exists till the end of a program. It kepp its value between multiple functions call.

The static variable has the default value zero (0) which is provided by compiler.

Program of static storage class in C++

#include <iostream>  
using namespace std;  
void func() {    
   static int i=0; //static variable    
   int j=0; //local variable    
   i++;    
   j++;    
   cout<<"i=" << i<<" and j=" <<j<<endl;    
}    
int main()  
{  
 func();    
 func();    
 func();    
}

Output

i= 1 and j= 1
i= 2 and j= 1
i= 3 and j= 1

4. Extern Storage Class

extern keyword is used to specifying a extern variable.

Extern storage class is define as that the variable is defined not within the same block where it is used. The value is assigned to it in a different block and this can be changed in a different block as well.Extern variable is same global variable initialized with a legal value. 

Extern is used if two or more than two files are sharing same variable or function.

Program of extern storage class in C++

#include <iostream>
using namespace std;
 
int x;
void externStorageClass()
{
 
    cout << " Extern class\n";
 
      extern int x;
 
    
    cout << "Value of the variable 'x'"
         << "declared, as extern: " << x << "\n";
 
    x = 2;
 
      
    cout<< "Modified value of the variable 'x'"
        << " declared as extern: \n"
        << x;
}
 
int main()
{
 
    externStorageClass();
 
    return 0;

Output

Extern class
Value of the variable 'x'declared, as extern: 0
Modified value of the variable 'x' declared as extern: 
2

 

5. Mutuable Storage Class

The mutable storage class specifier is modify one or more class data member through const function even though the member is part of an object declared as const. In this class nutable keyword is used to perfoming task.

Program of mutuable storage class in C++

#include <iostream>
using std::cout;
 
class Test {
public:
    int x;
     mutable int y;
 
    Test()
    {
        x = 4;
        y = 10;
    }
};
 
int main()
{
    const Test t1;
 
   
    t1.y = 20;
    cout << t1.y;
 
    return 0;
}

Output

20

 

Read more, Default argument in C++

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *