C++ Storage Classes

Storage Classes are used to provide behavior of variables like scopes, initial value, accessibility of variables.

Type of storage class

There are 5 type of storage class in c++ programming.

static

Static is an keyword there are used to create an storage class. This are prefix before data type of variables and change normal variable to special variables. declare static storage class using of this syntax.

static data_type variable_name;
Key point Description
static keyword
default value integers variable zero and char are blank
Scope location of variable. local variable used locally. and globally declared variables are utilized entire the program
Life Till the end of program execution

default value of static variable is not garbage. see an example.

/*Display default value of storage class in C++*/
#include<iostream>
using namespace std;
int main(){
  //declare static variable
  static int age;
  static char status;
  static float salary;
  static double population;
  //display default value of static variables
  cout<<"default static int value : "<<age<<endl;
  cout<<"default static char value : "<<status<<endl;
  cout<<"default static float value : "<<salary<<endl;
  cout<<"default static double value : "<<population<<endl;
}
Output
default static int value : 0
default static char value :
default static float value : 0
default static double value : 0

The speciality of static storage class variable is compiler are allocate memory of only one time. And used of those variable during program execution. View another example.

//Scope of static variable in c++
#include<iostream>
using namespace std;
void change(){
  //declare static variable
  static int calling=0;
  calling++; //increment value by one
  //display variable value
  cout<<calling<<endl; 
}
int main(){
  //calling function
  change();
  change();
  change();
}
Output
1
2
3

Note that in this program static variable is declared inside a function (change function). And executed (calling) of this function are three time. Observer that static variable value are create only one time. And modifies those value during program execution.

auto

auto storage class is default storage class that are used to defined normally of local variables within the sources code.

auto data_type variable_name;
Key point Description
auto keyword
default value Garbage value
Scope location of variable. local variable used locally. and globally declared variables are utilized entire the program
Life Block level

View an example of auto storage class.

#include<iostream>
using namespace std;
int main(){
  
  int x=100;
  {
    //string value
    auto x="This an strings";
    cout<<"auto x :"<<x<<endl;
    {
      //float value
      auto x=15.35f;
      cout<<"auto x :"<<x<<endl;
    }
  }
  cout<<"int x : "<<x;
  return 0;
}
Output
auto x :This an strings
auto x :15.35
int x : 100

register

register is an keyword to declare of storage class. this type of variable memory are create in RAM area. this type of variable are execution is faster to normal variable.

register data_type variable_name;
Key point Description
register keyword
default value Garbage value
Scope location of variable. local variable used locally. and globally declared variables are utilized entire the program
Life Block level

For example

#include<iostream>
using namespace std;
int main(){
  //register storage variable
  register int x=10,y=30;
  cout<<"Sum is: "<<x+y;
  return 0;
}
Output
Sum is: 40

C++ are allowed to create any data type in register storage class. but avoid to not create arrays and all variable as register. because register are has limit. and good program are create few veriables in register those variable are most of time are iterate in source code.

extern

Help of extern storage class we are capable to utilize global variable to inside of function. In case global variable are defined of anywhere within the program.

Key point Description
extern keyword
default value integers variable zero and char are blank
Life Till the end of program execution

View an example.

//extern storage class example in C++
#include<iostream>
using namespace std;

int main(){
  //Try to access global variable
  extern int auxiliary,temp;
  cout<<"auxiliary :"<<auxiliary<<endl;
  cout<<"temp :"<<temp;
}
int auxiliary=10,temp=20;
Output
auxiliary :10
temp :20

mutable

mutable storage class are capable to modified data member value of within constant class function and also can change the data member value of constant object.

constant member function are not allowed to modified data member value of any object. but some situation we need to modify the value then this are possible by mutable class.

look at this example compiler are not allowed to modified the value of data member within the constant function. that are producing an compilation error.

#include<iostream>
using namespace std;
class example
{
public:
  //data member
  int x,y;
  void set(int x,int y) const{
    this->x=x;
    this->y=y;
  }
  void get(){
    cout<<"x:"<<x<<" y:"<<y<<endl;
  }
};
int main(){
  //create an object of class example
  example e1;
  //call constant member function set function
  e1.set(20,10);
  //call member function
  e1.get();
  return 0;
}
Error
error: assignment of member 'example::x' in read-only object
     this->x=x;
             ^
error: assignment of member 'example::y' in read-only object
     this->y=y;
             ^

Observed that in this program compiler are not allowed to modified data member value. then how to solve this problem?. solve this problem by mutable storage class. view this example.

//example of mutable storage class in c++
#include<iostream>
using namespace std;
class example
{
public:
  //mutable variable
  mutable int x,y;
  int z;
  void set(int x,int y) const{
    /*
      not allow to edit z variable here
      because that is not mutable.
      // z=10; 
    */
    this->x=x;
    this->y=y;
  }
  void get(){
    cout<<"x:"<<x<<" y:"<<y<<endl;
  }
};
int main(){
  //create an object of class example
  example e1;
  //call constant member function set function
  e1.set(20,10);
  //call member function
  e1.get();
}
Output
x:20 y:10

Note that mutable keyword are added in before data member (mutable int x,y) of employee class. this are also used when modifying constant object value. view an example.

//example of mutable storage class in c++
#include<iostream>
using namespace std;
class employee
{
public:
  mutable float salary;
  float bonus; //normal variable
  employee(float,float);
  void display()const;
};
employee ::employee(float salary,float bonus){
  //set initial value to data member
  this->salary=salary;
  this->bonus=bonus;
}
void employee::display() const{
  //display data member of object
  cout<<"salary :"<<salary<<endl;
  cout<<"bonus  :"<<bonus<<endl;
}

int main(){
  //create an constant object of class employee
  const employee e1(50000.60f,10000.89f);
  e1.display(); //display data member value
  /*not allowed to change constant object value*/
  //e1.bonus=15000; //illegal
  e1.salary=60000.45f;//valid because mutable variable
  e1.display();
}
Output
salary :50000.6
bonus  :10000.9
salary :60000.4
bonus  :10000.9

Note that there are not possible to modify constant object data member value directly in c++ program. mutable storage class provide this feature to modified this value.

© 2019, regularcodes.com, All rights reserved