5 types of Storage Classes in C++ Programming

Spread the love

Storage classes in c++ determine the scope and lifetime of variables.

Storage Classes Variables tells you where to store them.

for eg. CPU, Register

This class is not a class of “C ++”. This is a concept derived from the “C” language, in which it is decided which data is being stored and how it is being used.

By using the Concept of Storage class, we decide which type of Variable we need in our program and what type of Variable we create to store the value in Memory.

INDEX –

  1. Types of Storage Classes
  2. Automatic Storage Class
  3. External Storage Class
  4. Register Storage Class
  5. Static Storage Class
  6. Mutable Storage Class
  7. Conclusion
  8. Frequently asked questions

There are 5 types of Storage Classes in C ++ Programming –

Storage Class

1. Automatic Storage Class –

In automatic storage class, we use the keyword ‘auto‘.

It is similar to the Normal Variable.

It is a Local Variable.

Their visibility or scope is within the function. They get destroyed outside.

Their default value is ‘garbage‘.

Variables that are declared within a block are useable, they are Automatic Variables.

The Syntax for Automatic Storage Class   

auto data_type variable_name = value(optional);

Variables are called Automatic we call a function,

all the Variables Declare in Function are created automatically and the Program Control exits Function Variables are Automatically Expired.

Example –

#include<iostream.h>
#include<conio.h>
 
void autoStorageClass()
{
 
            cout << ” auto class demo \n”;
 
            // Declaring an auto variable
            // No data-type declaration needed
            auto a = 24;
            auto b = 9;
 
 
            // printing the auto variables
            cout << a << ” \n”;
            cout << b << ” \n”;
}
 
void main()
{
clrscr();
 
            // demo auto Storage Class
            autoStorageClass();
 
     getch();
}

Output –

automatic storage class

2. External Storage Class –

Use  the ‘extern‘ keyword in External Storage Class.

The scope of external storage class variables is global.

Global Variable, are used with extern in the program inside function.

Their default value is ‘0‘.

Syntax for External Storage Class

extern data_type variable_name = value(optional);

We can use External Variables in Multi File Program.

we have made a Variable declare named MAX_LENGTH in the previous program.

It is a Global or External Variable. Any function can use Global Variable and these Variables are up to Termination of Lifetime Program.

Variables end only the Program Terminate.

any External Variable Create or Declare is created, it initializes 0.

Variables that are declared outside a function or class are called Variable of External Storage Class. These are called Global Variables.

use the value of an External Variable in other file.

we want to access an External Variable in a Multi-File Program,

If we declare an External Variable with an extern Keyword as follows,

we can access that External Variable in any other file. Such Variables are quite useful in Multi-File Programs.

Example –

#include<iostream.h>
#include<conio.h>
 
int a;
void externClass()
{
 
            cout << “Demonstrating extern class\n”;
 
            extern int a;
 
            // printing the extern variables ‘a’
            cout << “Value of the variable ‘a'”
                        << “declared, as extern: ” << a<< “\n”;
 
            // value of extern variable a modified
            a = 7;
 
            // printing the modified values of
            // extern variables ‘a’
            cout << “Modified value of the variable ‘x'”<< ” declared as extern: \n”
                        << a;
}
 
void main()
{
 
clrscr();
            // To demonstrate extern Storage Class
            externClass();
 
            getch();
}

Output –

External Storage Class

3. Register Storage Class –

In the Register Storage Class, we use the keyword ‘register‘.

The scope of Register Storage Class variables is Local.

Local Variable, they are used within the function in which they are declared or initialized in the program.

Register Storage Variables are stores on Computer’s Register.

The memory of the Register Storage Class is ‘Limited‘.

the memory of the register runs out, they are stored on CPU memory.

Register Variables have no address (&).

Their default value is ‘garbage‘.

Syntax for Register Storage Class

register data_type variable_name = value(optional);

Register Variable is a special type of Automatic Variable.

We want to declare a Variable of register class, we have to use register Keyword.

The Idea of the ​​Declaration of Register Variable is we declare a Variable of register class, Variable is created directly in the CPU Register instead of Create in Memory.

The values ​​of registers are much faster those of memory,

These greatly increase the speed of the Variable Program. The variable in which the InnerMost Loop is in a nested Loop is stored in the Variable Always Register Class.

Example –

#include<iostream.h>
#include<conio.h>
 
void registerClass()
{
 
            cout << “Demo register class\n”;
 
            // declaring a register variable
            register char x= ‘t’;
 
            // printing the register variable ‘x’
            cout << “Value of the variable ‘x'”
                        << ” declared as register: ” << x;
}
void main()
{
 
clrscr();
            // To demon rut-egister Storage Class
            registerClass();
     getch();
}

Output –

Register Storage

4. Static Storage Class –

In static storage class, we use the keyword ‘static‘.

The scope of Static Storage Class Variables is Local and Global.

Their default value is ‘0‘.

Syntax for Static Storage Class

static data_type variable_name = value(optional);

read also – what is operators?

The lifetime of the Local Static Variable extends to the entire program.

We Static Declare a Local Variable, Variable remains alive our Program is running.

Variables are destroyed we terminate our program.

The values ​​are inside Static Local Variables remain in the entire program. These are always initialized by declaring 0.

Example –

#include<iostream.h>
#include<conio.h>
// Function containing static variables
// memory is retained during execution
int staticFun()
{
            cout << ” static variables: “;
            static int count = 0;
            count++;
            return count;
}
 
// Function containing non-static variables
// memory is destroyed
int nonStaticFun()
{
            cout << ” Non-Static variables: “;
 
            int count = 0;
            count++;
            return count;
}
 
void main()
{
clrscr();
 
            // Calling the static parts
            cout << staticFun() << “\n”;
            cout << staticFun() << “\n”;
 
            // Calling the non-static parts
 
            cout << nonStaticFun() << “\n”;
          
            cout << nonStaticFun() << “\n”;
           
            getch();
}

Output –

Static Storage Class

 5. Mutable Storage Class –

A specifier that can be applied to the declaration of a non-static, non-reference data member of a class.

A mutable member of a class is not const even the object is const.

The mutable specifier applies only to class objects

It allows a member of an object to override const member function. a mutable member can be modified by a const member function.

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

source – edureka

Example –

#include<iostream>
using namespace std;
 
class tosif
{
    mutable int a;
    int b;
    public:
        tosif(int x,int y)
        {
            a=x;
            b=y;
        }
        void square_a() const
        {
            a=a*a;
        }
        void display() const
        {
            cout<<“a = “<<a<<endl;
            cout<<“b = “<<b<<endl;
        }
};
 
int main()
{
    const tosif x(7,9);
    cout<<“Initial value”<<endl;
    x.display();
    x.square_a();
    cout<<“Final value”<<endl;
    x.display();
    return 0;
}

Output –

Mutable Storage Class
LifetimeSpecifier( keywords)visibility (scope)Lifetime
 AutomaticautoLocalFunction
StaticStaticLocalProgram
RegisterRegisterLocalFunction
ExternalexternGlobalProgram
MutableMutableLocalclass

Conclusion –

Types of Storage Classes – Automatic Storage Class, External Storage Class, Register Storage Class, Static Storage Class, Mutable Storage Class all of them Syntax, examples, output I gave you above.

Friends, reading this post completely, you must have understood Storage Classes in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently asked questions

1. What are storage classes?

 Storage Classes Variables tells you where to store them.

2. Difference between data type and storage class?

Data types refer to the type of information storage class refers to the lifetime of a variable and its scope within the program.

3. What is an automatic storage class?

In automatic storage class, we use the keyword ‘auto’.
It is similar to the Normal Variable.
It is a Local Variable.
Their visibility or scope is within the function. They get destroyed outside.
Their default value is ‘garbage’.
Variables that are declared within a block are useable, they are Automatic Variables.

4. What is the extern storage class?

Extern storage class is used we have global functions or variables which are shared two or more files. The Keyword extern is used to declaring a global variable or function in other files to provide the reference of variable or function which has been already defined in the original file.

5. What is the static storage class?

In static storage class, we use the keyword ‘static’.
the lifetime of the Local Static Variable extends to the entire program.
we Static Declare a Local Variable, Variable remains alive our Program is running.
variables are destroyed we terminate our program.
the values ​​are inside Static Local Variables remain in the entire program. These are always initialized by declaring 0.

6. What is the register storage class?

The register storage class is used to define local variables that should be stored in a register of RAM. This means that the variable has a maximum size equal to the register size (usually one word) and can’t have the unary ‘&’ operator applied to it (as it does not have a memory location).

7. What is a mutable storage class in C++?

Leave a Comment