Storage Classes in C++

Storage Classes in C++: The storage classes in C++ defines the scope and lifetime of a variables (or) functions within a program. Here, Lifetime refers to the period during which a variable remains active and visibility refers to the module of the program. So that a variable can be accessible. These specifiers precede the type that they are going to modify. There are different types of storage classes available in C++. They are as follows:

  • auto (or) automatic in C++
  • register in C++
  • static in C++
  • extern in C++
  • mutable in C++

Storage Classes in C++

Storage Class Keyword Lifetime Visibility Initial Value
Automatic auto Function Block Local Garbage
Register register Function Block Local Garbage
Mutable mutable Class Local Garbage
External extern Whole Program Global Zero
Static static Whole Program Local Zero

 1) auto (or) Automatic Storage Class in C++

The auto storage class in C++ is the default variable for all the local variables. And these variables are defined within the body function are called “auto variable”.

Syntax:

datatype var_name1 [= value];
    or
auto datatype var_name1 [= value];

Example:

{   
auto int y;  
float y = 3.12;  
}

From this example, we can see that you have declared two variables, with the same storage class.

2) register Storage Class in C++

In the register storage class in C++, it is used for defining the local variables that should be stored in the register instead of RAM. Its size is same as register size. And also it is having fast access than other variables. So, we recommend using register for fast access such as a counter.

Syntax: register datatype var_name1 [= value];

Example:

register int counter=0;

We cannot confirm that the register is going to store the variable in a register. It depends upon the hardware and implementation instructions.

3) static storage class in C++

The static storage class in C++  is initialized only once and exists until the end of the program. If you are making a local variable in existence during the lifetime of the program instead of creating and destroying it every time. so, that it comes into and goes out of the scope.’

In C++ when static is used on class data member, it causes only one copy of that member to be shared by all objects of its calls.

Syntax:static datatype var_name1 [= value];

Example:

#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 is 6 and count is 9
i is 7 and count is 8
i is 8 and count is 7
i is 9 and count is 6
i is 10 and count is 5
i is 11 and count is 4
i is 12 and count is 3
i is 13 and count is 2
i is 14 and count is 1
i is 15 and count is 0

4) extern Storage Class in C++

The extern storage class in C++ is used to give a reference to a global variable that is visible to all the program files. When you are using the ‘extern ‘ the variable cannot be initialized as it is not pointing the variable at the storage location.

mainly the extern modifier is used when there are two or more files sharing the same global variables (or) the functions.

Syntax: register datatype var_name1 [= value];

Example: First File: main.cpp

#include <iostream>
int count ;
extern void write_extern(); 
main() {
   count = 5;
   write_extern();
}

Example: Second file: support.cpp

#include <iostream>
extern int count;
void write_extern(void) {
   std::cout << "Count is " << count << std::endl;
}

Output: 5

5) Mutable Storage Class in C++

The mutable storage class in C++ specifier allows only to the class objects. And the mutable member can be modified by constant member function. It also allows a member of an object to override constant member function.

Syntax: mutable datatype var_name1;

Example:

#include<iostream>
using namespace std;

class test
{
    mutable int a;
    int b;
    public:
        test(int x,int y)
        {
            a=x;
            b=y;
        }
        void square_a() const
        {
            a=a*a;
        }
        void display() const
        {
            cout<<"a = "<<a<<endl;
            cout<<"b = "<<b<<endl;
        }
};

int main()
{
    const test x(2,3);
    cout<<"Initial value"<<endl;
    x.display();
    x.square_a();
    cout<<"Final value"<<endl;
    x.display();
    return 0;
}

Output:

Initial value
a = 2
b = 3
Final value
a = 4
b = 3