Updated March 20, 2023
Introduction to Storage Class in C++
The following article gives a description of the Storage Class in C++. When we define any variable in C++ each variable has a data type to make the user understand what kind of variable it is. We haven’t defined any storage classes yet because we were using by default Storage classes. As compiler automatically assigned a storage class by default for defining a variable. To define the feature of a method or variable in programming Storage classes are used. Features like life span, visibility and scope can be defined. Lifetime means the duration of variable activity and visibility means which part of code can access by the particular variable based on visibility given.
Types of Storage Class
There are usually 5 types of storage classes available in the C++ programming language. Let’s have a look at all of them explained with easy examples:
1. Automatic Storage Class
For all the local variables automatic class is the by default class for storage. We all are using automatic class since we started programming. If a variable is declared without any keyword inside a function, it is automatic by default otherwise auto keyword is used.
Syntax:
datatype var_name1 [= value]; // by default if you don’t use auto keyword
auto datatype var_name1 [= value];
For Example:
auto int x;
auto float x =68.59;
C ++ code to demonstrate automatic storage class concept:
#include <iostream>
using namespace std;
void autoStorageClass() {
cout << "Implementing auto storage class concept \n";
// Declaring an automatic variable named as "auto "
// No data-type declaration needed
auto p = 400;
auto q = 400.35;
auto r = "eduCBA";
auto s = 'F';
cout << p <<"\n";
cout << q <<"\n";
cout << r <<"\n";
cout << s <<"\n";
// printing the auto variables through cout
}
int main()
{ // To implement auto Storage Class
autoStorageClass();
return 0;
}
Output:
2. Static Storage Class
When we want our variable visibility to be the same as a local variable but the lifetime of an external variable. The scope of a static variable doesn’t die even if the function execution is over. The default value of the static variable is 0. Static storage class is used to save recursive function values in a program.
Syntax:
static datatype var_name1 [= value];
For Example
static int r = 1;
static float total;
C ++ code to demonstrate static storage class concept:
#include <iostream>
// declaring the function
void function(void);
static int count = 10; //defining a global variable
main() {
while(count--) {
function();
}
return 0;
}
// defining the function
void function( void ) {
static int x = 50; // local static variable
x++;
std::cout << "The value of x is " << x ;
std::cout << " And the count is " << count << std::endl;
}
Output:
3. Mutable Storage Class
Mostly used when you don’t want to change the information but state of the function or program. In the bank, money transactions should be locked but when you are transacting money it states should be changed to processing from started then completing. To modify the class object during program execution Mutable storage classes is used.
Syntax:
mutable datatype var_name1;
Example
mutable int y;
mutable char c;
C ++ code to demonstrate mutable storage class concept:
#include<iostream>
using namespace std;
// defining a class 'A'
class A {
public :
A (int x, int y) { // declaring a constructor with same class name
m = x;
n = y; // initializing the data members
}
int m;
mutable int n; // declaring the data members
};
int main() {
const A obj(50, 22);
cout << "m : " << obj.m << " n : " << obj.n << endl;
// obj.m = 70;
obj.n = 80;
cout << "m : " << obj.m << " n : " << obj.n << endl;
return 0;
}
Output:
4. Register Storage Class
To allow faster access in C ++ programming values of the class register are stored in a CPU register. The variable scope is local to function and it dies as soon as execution completed. That’s how it has faster access as the scope is not needed once work is done. Register class poses similar functionalities like auto storage class.
Syntax:
register datatype var_name1 [= value];
Example
register int rollnumber;
register char c;
C ++ code to demonstrate register storage class concept:
#include <iostream>
using namespace std;
void registerStorageClass()
{
cout << "Demonstrating the register class concepts \n ";
// register variable declaration
register char a = 'C';
// printing the register variable 'a'
cout << "Value of the variable 'a'"
<< " declared as register: " << a;
}
int main()
{ registerStorageClass();
return 0;
}
Output:
5. External Storage Class
Simple and easy storage class that defines that variables are defined not in the same block where it is used. As it assigns a variable reference to a global variable which is usually declared outside the code. As variables of this class are “ Global Variables”.
Syntax:
extern datatype var_name1;
Example
extern float var2;
C ++ code to demonstrate extern storage class concept
#include <iostream>
using namespace std;
int i;
void externStorageClass()
{
cout << "Demonstrating the extern storage class concept \n";
extern int i;
cout << "The Value of the variable 'i'"
<< "declared, as extern: " << i << "\n";
i = 85;
cout << "Modified value of the variable 'i'" << " declared as extern: \n" << i;
}
int main()
{
// To implement extern Storage Class
externStorageClass();
return 0;
}
Output:
Conclusion
The storage class concept is very much useful in C++ programming language because a user can define the visibility of every single variable in the program. Apart from that, a user can decide the life-time of variables using these 5 types of storage classes depending on the requirement in the code.
Recommended Articles
This has been a guide to Storage Class in C++. Here we discuss the basic concepts and different types of storage class in c++ with syntax and examples. You may also have a look at the following articles to learn more –