Wednesday, 5 November 2014

Storage classes available in C++

Storage classes available in C++: 

The storage classes are useful to us to specify the scope of the variables.
In C++ we have five different types available:
Automatic/Local Variables, keyword is auto

Mutable variables, keyword is mutable

Static Variables, keyword is static

Register Variables, keyword is register

Extern Variables, keyword is extern

Automatic / Local Variables:

This is the default storage class in C++.  The name itself is automatic. So the scope of these variables are entered automatically and end same way. That’s why these are called as automatic variables.
Here is an example
double dVal;

auto double dVal

Both the declarations are same. There is no need to add the auto before the variable declaration.

Mutable Storage class:

It is applicable only for the class objects. It is used to modify the const member or member function in the class. For example we have the class called car it has the model, owner, mileage are the variables, which are const. means the features of the car is same, in feature the owner may change. To change the owner variable we need to make the owner of the car class to be mutable. Then only we can change the owner information in the feature.

Static storage class:

This is used to keep the local variable in existence during the life time of the program.This is possible only when we declare the variable as static. Static keyword allows us to maintain their values between function calls.

#include <iostream>

  /* Function declaration */

void MyStaticFun(void);

static int iCnt = 5; /* Global variable */


    while(iCnt --)
       MyStaticFun ();
    return 0;

// Function definition
void MyStaticFun ( void )
    static int iVal = 5; // local static variable
    std::cout << " iVal is " << i ;
    std::cout << " and count is " << iCnt << std::endl;
When the above code is compiled and executed, it produces the following output:
iVal is 6 and count is 4
iVal is 7 and count is 3

iVal is 8 and count is 2

iVal is 9 and count is 1

iVal is 10 and count is 0

Extern storage classes:

This storage class is used to access the variable of the one class declared as global can be used to in the another class, and it will allow us to access that in all the classes in the project. i.e to share the common data in multiple files.
When you have multiple files and you define a global variable or function, which will be used in other files also, then extern will be used in another file to give reference of defined variable or function. Just for understanding, extern is used to declare a global variable or function in another file.

#include <iostream>
Using namespace std;
Int igVar;  // global variable
Class classA {
// your code goes here
Extern int igVar;
Int iVal = igVar + 20;
The global variable igVar is declared in the First.cpp file, which is used in the Second.cpp file using extern keyword.
Register storage classes:

This storage class is used to define the local variables, which are going to store into the registers. Basically the local variables are stored in the RAM. There is a limitation that the size of the variable should be the size of the Register, that is normally a word.
The register variables are decalred like below
register int irVariable; 

But the register variables are no0t stored in the memory location and it cannot have the & operator applied to it.
 The major uses of these register variables are those are having the quick access.

No comments: