Programming languages - C++

Back to Course

Lesson Description

Lession - #40 C++ Storage Classes

A storage class characterizes the scope (perceivability>
and life-season of variables and additionally functions inside a C++ Program. These specifiers go before the kind that they change. There are following storage classes, which can be utilized in a C++ Program

  • auto
  • register
  • static
  • extern
  • mutable



The auto Storage Class

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



   int mount;
   auto int month;



The register Storage Class

The register class is utilized to characterize local variables that ought to be put away in a register rather than RAM. This implies that the variable has a greatest size equivalent to the register size (generally single word>
and can't have the unary 'and' operator applied to it (as it doesn't have a memory location>





   register int  miles;





The static Storage Class

The static storage class teaches the compiler to keep a local variable in presence during the life-season of the program as opposed to making and obliterating it each opportunity it comes into and leaves scope. Hence, making local factors static permits them to keep up with their qualities between function calls.





// Function declaration
void func(void>
; static int count = 10; /* Global variable */ main(>
{ while(count-->
{ func(>
; } return 0; } // Function definition void func( void >
{ static int i = 5; // local static variable i++; std::cout << "i is " << i ; std::cout << " and count is " << count << std::endl; }





The extern Storage Class

The extern storage class is utilized to give a reference of a global variable that is noticeable to ALL the program files. Whenever you use 'extern' the variable can't be instated as everything it does is point the variable name at a storage location that has been recently characterized.

At the point when you have different files and you characterize a global variable or function, which will be utilized in different files additionally, then, at that point, extern will be utilized in one more files to give reference of characterized variable or capacity. Only for understanding extern is utilized to proclaim a global variable or function in another file.





First File: main.cpp



int count ;
extern void write_extern(>
; main(>
{ count = 5; write_extern(>
; }



Second File: support.cpp




extern int count;

void write_extern(void>
{ std::cout << "Count is " << count << std::endl; }



The mutable Storage Class

The mutable specifier applies just to class objects, which are talked about later in this instructional exercise. It permits an individual from an item to supersede const part function. That is, an mutable part can be changed by a const part function.

The c++ & symbol is used as an operator in C++.
geeksforgeeks C++ is a general-purpose programming language and is widely used nowadays for competitive programming.
The major purpose of oops in C++ programming is to introduce the concept of object orientation to the C programming language.
turbo c++ download for windows 10 and Windows 11 (32-64 bit>
with full-screen mode and many more extra features.