Storage classes in C++
Storage classes in c++ is explained with different class variables. In C++ programming language we have multiple files. In those files we may have normal variables, array, functions, structures, unions, classes etc. So, variables and objects declared must have the visibility, the lifetime and the storage, when values assigned.
Storage class defined for a variable determines the accessibility and longevity of the variable. The accessibility of the variable relates to part of the program that has access to the variable. The longevity of the variable refers to the length of time the variable exists within the program. It tell the compiler whether the duration and visibility of the variables or objects declared, and also where the variables or objects should be stored.
Types of Storage Class Variables in C++:
- Automatic
- External
- Static
- Register
Automatic Storage Class
The same variable names may be declared and used in variant functions.They are only known in the functions in which they are declared. That means, there is no confusion even if the same variables names are declared and used in different functions.
auto int x, y, z = 30;
similarly,
int x, y, z = 30;
External Storage Class
Rather than locally, External variables are variables are recognized globally. In other words, once if declared, the variable can be used in any line of program throughout the rest of the code. A variable defined outside a function is external. An external variable can also be declared within the function that uses it by using the keyword extern hence it can be accessed by other code in other files.
The external storage class is commonly used when there are two or more files sharing the same global variables or the functions as explained below.
First File : first.cpp
#include <iostream.h>
#include “second.cpp”
int count ;
extern void display();
void main()
{
display();
}
Second File: second.cpp
#include <iostream.h>
extern int count;
void display()
{
count = 5;
cout << “Count is ” << count << endl;
}
Register Storage Classes
The register storage class is used to define local variables that should be stored in a register instead of RAM. The register should only be used for variables that require quick access such as counters.
Register variables, like automatic variables, are local to the function in which they are declared. Defining certain variables to be register variables does not, however, guarantee that they will actually be treated as register variables. Registers will be assigned to these variables by compiler so long as they are available. If a register declaration cannot be fulfilled, the variables will be treated as automatic variables. So, it is not a mandatory for the compiler to fulfill the register variables.
The keyword used to declare a register variable is ‘register‘ .
Static Storage Class
The static storage class instructs the compiler to keep a local variable in existence during the lifetime of the program instead of creating and destroying it whenever it comes into and goes out of scope. Therefore, making local variables static permits them to maintain their values between function calls.
‘static’ is the keyword used to declare a static variable.
Static variables are local variables that retain their values throughout the lifetime of the program. In other words, their same (or the latest) values are still available when the function is re-invoked later. Their values can be utilized within the function in the same manner as other variables, but they can’t be accessed from outside of their defined function.
The static modifier may also be applied to global variables. When it is over, it causes that variable’s scope to be restricted to the file in which it is declared.
In C++, when static is used on a class data member, it causes only one copy of that member to be shared by all objects of its class.
#include <iostream.h>
void increment();
void main()
{
increment();
increment();
increment();
}
void increment ()
{
static int i = 0;
i++;
cout << “A is ” << i <<endl;
}
The output is :
A is 1
A is 2
A is 3