storage class
Storage Classes are used to describe the features of a variable/function. These features basically include the scope, visibility and life-time which help us to trace the existence of a particular variable during the runtime of a program.
C++ uses 5 storage classes, namely:
auto
register
extern
static
mutable
1. auto:
=========
The auto keyword provides type inference capabilities, using which automatic deduction of the data type of an expression in a programming language can be done.
It is the default storage class for all local variables. The auto keyword is applied to all local variables automatically.
{
auto int y;
float y = 3.45;
}
The above example defines two variables with a same storage class, auto can only be used within functions.
2.register:
==========
This storage class declares register variables which have the same functionality as that of the auto variables.
The only difference is that the compiler tries to store these variables in the register of the microprocessor
if a free register is available. This makes the use of register variables to be much faster than that of the variables stored in the memory during the runtime of the program. If a free register is not available,
these are then stored in the memory only.
Usually, a few variables which are to be accessed very frequently in a program are declared with the register keyword which improves the running time of the program
The register variable allocates memory in register than RAM. Its size is same of register size. It has a faster access than other variables.
It is recommended to use register variable only for quick access such as in counter.
Note: We can't get the address of register variable.
register int counter=0;
3. Extern storage class
simply tells us that the variable is defined elsewhere and not within the same block where it is used.
Basically, the value is assigned to it in a different block and this can be overwritten/changed in a different block as well.
External Storage Class
The extern variable is visible to all the programs. It is used if two or more files are sharing same variable or function.
=========
#include <iostream>
using namespace std;
// declaring the variable which is to
// be made extern an intial value can
// also be initialized to x
int x;
void externStorageClass()
{
cout << "Demonstrating extern class\n";
// telling the compiler that the variable
// x is an extern variable and has been
// defined elsewhere (above the main
// function)
extern int x;
// printing the extern variables 'x'
cout << "Value of the variable 'x'"
<< "declared, as extern: " << x << "\n";
// value of extern variable x modified
x = 2;
// printing the modified values of
// extern variables 'x'
cout
<< "Modified value of the variable 'x'"
<< " declared as extern: \n"
<< x;
}
int main()
{
// To demonstrate extern Storage Class
externStorageClass();
return 0;
} 0,2 0utput
====================================
extern int counter=0;
=========================
4.static:
=======
This storage class is used to declare static variables which are popularly used while writing programs in C++ language.
Static variables have a property of preserving their value even after they are out of their scope!
qaHence, static variables preserve the value of their last use in their scope.
==================================
The static variable is initialized only once and exists till the end of a program. It retains its value between multiple functions call.
The static variable has the default value 0 which is provided by compiler.
#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
===================================
5.mutable: Sometimes there is a requirement to modify one or more data members of class/struct through const function even though you don’t want the function to update other members of class/struct.
This task can be easily performed by using the mutable keyword. The keyword mutable is mainly used to allow a particular data member of const object to be modified.
Comments
Post a Comment