Storage Classes in C++ | auto,,register, static,extern,mutable | My CS Tutorial - My CS Tutorial

Breaking

Programming languages full tutorial and programs, exam papers, visual basic( Vb.net ), information of new technologies and more....

Friday, July 17, 2020

Storage Classes in C++ | auto,,register, static,extern,mutable | My CS Tutorial

Storage classes in c++ | C++ storage classes | auto,register, static, extern and mutable | different types of storage classes in c++ programming | My CS Tutorial

Storage classes in c++ My CS Tutorial

STORAGE CLASSES IN C++


A storage class defines the scope (visibility) and life-time of variables and/or functions within a C++ Program. These specifiers precede the type that they modify. There are following storage classes, which can be used in a C++ Program:-

(i) auto

(ii) register

(iii) static

(iv) extern

(v) mutable

THE AUTO STORAGE CLASS


The auto storage class is the default storage class for all local variables.

{

 int mount;

 auto int month;

}

The example above defines two variables with the same storage class, auto can only be used within functions, i.e., local variables.

THE REGISTER STORAGE CLASS


The register storage class is used to define local variables that should be stored in a register instead 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).

{

 register int miles;

}

The register should only be used for variables that require quick access such as counters. It should also be noted that defining 'register' does not mean that the variable will be stored in a register. It means that it MIGHT be stored in a register depending on hardware and
implementation restrictions.

THE STATIC STORAGE CLASS


The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.

The static modifier may also be applied to global variables. When this is done, 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.

THE EXTERN STORAGE CLASS


The extern storage class is used to give a reference of a global variable that is visible to all the program files. When you use 'extern' the variable cannot be initialized as all it does is point the variable name at a storage location that has been previously defined.

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.

The extern modifier is most commonly used when there are two or more files sharing the same global variables or functions as explained below.

First File: main.cpp

#include <iostream>

int count ;

extern void write_extern();

main()

{

 count = 5;
 write_extern();

}

Second File: support.cpp

#include <iostream>

extern int count;

void write_extern(void)

{

 std::cout << "Count is " << count << std::endl;

}

Here, extern keyword is being used to declare count in another file. Now compile these two files as follows:-

main.cpp support.cpp -o write

This will produce write executable program, try to execute write and check the result as follows:-
5


THE MUTABLE STORAGE CLASS


The mutable specifier applies only to class objects.
It allows a member of an object to override const member function. That is, a mutable member can be modified by a const member function.
The mutable storage class specifier is used only on a class data member to make it modifiable even though the member is part of an object declared as const. You cannot use the mutable specifier with names declared as static or const, or reference members.


Storage classes in c++ | C++ storage classes | auto,register, static, extern and mutable | different types of storage classes in c++ programming | My CS Tutorial
_______________________________________


Please share this post and blog link with your friends.For more programs use this blog.

If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com

Created by-- HARSH CHAUHAN

No comments:

Post a Comment