Below are the different Storage Classes available in C++. We shall see all these storage classes in this chapter.
- Automatic Storage Class
- External Storage Class
- Static Storage Class
- Register Storage Class
- Mutable Storage Class
1. Automatic Storage Class
Keyword: auto Lifetime: Function Block Visibility: Local Initial Value: Garbage
It is the default storage class when you declare a local variable inside a function.
Once the execution of the function is finished, the variable is destroyed.
Syntax:
<data_type> <variable_name>; auto <data_type> <variable_name>;
Example:
auto int a = 10; or auto int b;
2. External Storage Class
Keyword: extern Lifetime: Till program Execution Visibility: Global Visibility Initial Value: Zero
When you are accessing a global variable defined in one fine and accessing in another file, then we need to use “extern” storage class.
It is just an information to the programmer that the variable has been declared in another file and using in this file.
Syntax:
extern <data_type_name> <variable_name>;
Example:
extern int num;
Example:
//file_one.cpp int num=50; // assigning value to test void multiply(int n) { num=num*n; } //file_two.cpp #include<iostream> #include "file_one.cpp" // includes the content of file_one.cpp using namespace std; extern int num; // declaring num as extern variable int main() { cout<<num<<endl; multiply(5); cout<<num<<endl; return 0; }
3. Static Storage Class
Keyword: static Lifetime: Till program Execution Visibility: Global Visibility Initial Value: Zero
Static variables can be declared inside a function or as a global variable. But the lifetime of the variable will be till the execution of the program.
Local static variable will maintain its value from one function call to another function call.
Global static variable is used to make that variable local to that file. It cannot be accessible from another file, as we have made that variable as static.
Syntax:
static <data_type> <variable_name>;
Example:
static int count;
Programming Example:
#include<iostream> using namespace std; void func() { static int count = 0; count++; cout << "Function is called " << count << " times " << endl; } int main() { func(); func(); func(); func(); return 0; }
Output:
Function is called 1 times Function is called 2 times Function is called 3 times Function is called 4 times
4. Register Storage Class
Keyword: register Lifetime: Function block Visibility: Local Initial Value: Garbage value
It is a request for the compiler that the variable is used frequently, hence store the variable in CPU Register rather than memory for faster accessibility.
But it depends on the running time whether the variables are stored in CPU registers or in memory.
Syntax:
register <data_type> <variable_name>;
Example:
register int num;
5. Mutable Storage Class
Keyword: mutable Lifetime: Class Visibility: Local Initial Value: garbage value
It is possible when you create an object of a class to be made as constant. It means, that object will not be able to modify the data members.
But there will be some times, even if the object is made as const, we need to modify a data member. In such cases, we need to make that variable as mutable, using “mutable” keyword.
Example:
#include<iostream> using namespace std; class Mutable_Test { mutable int num_1; int num_2; public: Mutable_Test(int x,int y) { num_1 = x; num_2 = y; } void double_num_1() const { num_1 = num_1 * num_1; } void display() const { cout<<"num_1 = "<<num_1<<endl; cout<<"num_2 = "<<num_2<<endl; } }; int main() { const Mutable_Test mutable_obj(2,3); cout<<"Initial value"<<endl; mutable_obj.display(); //change the value of num_1 mutable_obj.double_num_1(); cout<<"Final value"<<endl; mutable_obj.display(); return 0; }
Output:
Initial value num_1 = 2 num_2 = 3 Final value num_1 = 4 num_2 = 3