C++ Qualifiers and Storage Classes - Devbhoomi

FREE JOB ALERT & ONLINE TUTORIALS

Hot

Post Top Ad

Sunday 8 October 2017

C++ Qualifiers and Storage Classes

C++ Qualifiers and Storage Classes



ualifiers and storage class are smaller but important programming concept that helps to make the quality of a variable more accurate for using that variable within the program. In this chapter you will learn about how qualifiers are used with variables and what the roles of different storage classes in C++ are.

What is a Qualifier?

A qualifier is a token added to a variable which adds an extra quality, such as specifying volatility or constant-ness to a variable. They act like an adjective for a variable. With or without a qualifier, the variable itself still occupies the same amount of memory, and each bit has the same interpretation or contribution to the state/value. Qualifiers just specify something about how it may be accessed or where it is stored.
Three qualifiers are there in C++. These are:
  • const: This is used to define that the type is constant.
  • volatile: his is used to define that the type is volatile.
  • mutable: applies to non-static class members of non-reference non-const type. Mutable members of const classes are modifiable.

Storage Class in C++

Storage classes are used to classify the lifetime and scope of variables. It tells how storage is allocated for variables and how variable can be treated by complier; everything depends on these storage classes. To fully define a variable, storage classes are required apart from data type. Till now you haven’t used any of the storage class, but the default storage class was there playing its role secretly. Auto is the default storage class.
These storage classes are divided into four types. They are:
  • Auto Variables
  • Register variables
  • Static Variables
  • Extern Variables

auto

This is the C++’s default storage class you have been using or the compiler is automatically assigning it to the variables. It is applied to local variables and those variables are visible only within the function inside which it is declared and it gets terminated as soon as the function execution gets over. The variables having “auto” as their storage class contains garbage value if they are not assigned any value. The keyword used is “auto”.
Example:
int var1;          // by default, storage class is auto
auto int var;

register

The register storage class is used to classify local variables that gets stored in the CPU register instead of primary memory (RAM) which means that the variable has a maximum size equal to the register size (usually one word) and cannot have the unary ‘&’ operator applied to it (as it does not have a memory location). The storage class register is used only with those variables that require quick access such as counters. It is not a must that a variable declared with register will always be stored in the CPU register; it means it might get stored in CPU register which fully depends on hardware and implementation restriction. The keyword “register” is used for declaring register variables.
Example of declaration:
Example:
register int var2;

static

The scope of static variable is local to the function in which it is defined but it doesn’t get terminated when the function execution gets over. In other words, the static storage class tells the compiler to keep the local variable in existence during the life-time of the program instead of destroying it with the end of function scope. In between the function calls, the value of the static variable persists. By default, the static variable stores zero (0) as its initial value. The static modifier may also be applied to global variables.
Example:
static int var3 = 6;

extern

Variables having extern storage class have a global scope. Extern variables are used when programmers want a variable to be visible outside the file in which it is declared. Variables with storage class extern can be shared across multiple files. Extern variables do not end until the program execution gets terminated.
Example:
extern int var4; // declaration of variable 'var4'
int var4;          // definition of variable 'var4'

Program for static Storage Class

#include <iostream>
using namespace std; 

void fun()
{
   static int i = 6;
   i++;
   cout << i;
}

int main()
{
   fun();
   fun();
   fun();
}

Program for register Storage Class

#include <iostream>
using namespace std; 

int main()
{
   int a,b;
   regster int c;
   cout<<"Enter the two values";
   cin>>a>>b;
   c=a+b;
   cout<<"Tot is:"<<c;
}




No comments:

Post a Comment

Post Top Ad