EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Storage Class in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Storage Class in C++

Storage-Class-in-C++

Introduction to Storage Class in C++

The following article gives a description of the Storage Class in C++. When we define any variable in C++ each variable has a data type to make the user understand what kind of variable it is. We haven’t defined any storage classes yet because we were using by default Storage classes. As compiler automatically assigned a storage class by default for defining a variable. To define the feature of a method or variable in programming Storage classes are used. Features like life span, visibility and scope can be defined. Lifetime means the duration of variable activity and visibility means which part of code can access by the particular variable based on visibility given.

Types of Storage Class

There are usually 5 types of storage classes available in the C++ programming language. Let’s have a look at all of them explained with easy examples:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Types of Storage Class

1. Automatic Storage Class

For all the local variables automatic class is the by default class for storage. We all are using automatic class since we started programming. If a variable is declared without any keyword inside a function, it is automatic by default otherwise auto keyword is used.

Syntax:

datatype var_name1 [= value]; // by default if you don’t use auto keyword
auto datatype var_name1 [= value];

For Example:

auto int x;
auto float x =68.59;

C ++ code to demonstrate automatic storage class concept:

#include <iostream>
using namespace std;
void autoStorageClass() {
cout << "Implementing auto storage class concept \n";
// Declaring an automatic variable named as "auto "
// No data-type declaration needed
auto p = 400;
auto q = 400.35;
auto r = "eduCBA";
auto s = 'F';
cout  <<  p <<"\n";
cout  <<  q <<"\n";
cout  <<  r <<"\n";
cout  <<  s <<"\n";
// printing the auto variables through cout
}
int main()
{ // To implement auto Storage Class
autoStorageClass();
return 0;
}

Output:

stoorage class in c++1

2. Static Storage Class

When we want our variable visibility to be the same as a local variable but the lifetime of an external variable. The scope of a static variable doesn’t die even if the function execution is over. The default value of the static variable is 0. Static storage class is used to save recursive function values in a program.

Syntax:

static datatype var_name1 [= value];

For Example

static int r = 1;
static float total;

C ++ code to demonstrate static storage class concept:

#include <iostream>
// declaring the function
void function(void);
static int count = 10;  //defining a global variable
main() {
while(count--) {
function();
}
return 0;
}
// defining the function
void function( void ) {
static int x = 50; // local static variable
x++;
std::cout << "The value of x is " << x ;
std::cout << " And the count is " << count << std::endl;
}

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,837 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Output:

stoorage class in c++ 2 PNG

3. Mutable Storage Class

Mostly used when you don’t want to change the information but state of the function or program. In the bank, money transactions should be locked but when you are transacting money it states should be changed to processing from started then completing. To modify the class object during program execution Mutable storage classes is used.

Syntax:

mutable datatype var_name1;

 Example

mutable int y;
mutable char c;

C ++ code to demonstrate mutable storage class concept:

#include<iostream>
using namespace std;
// defining a class 'A'
class A {
public :
A (int x, int y) { // declaring a constructor with same class name
m = x;
n = y;    // initializing the data members
}
int m;
mutable int n;   // declaring the data members
};
int main() {
const A obj(50, 22);
cout << "m : " << obj.m << "  n : " << obj.n << endl;
// obj.m = 70;
obj.n = 80;
cout << "m : " << obj.m << "  n : " << obj.n << endl;
return 0;
}

Output:

type 3

4. Register Storage Class

To allow faster access in C ++ programming values of the class register are stored in a CPU register. The variable scope is local to function and it dies as soon as execution completed. That’s how it has faster access as the scope is not needed once work is done. Register class poses similar functionalities like auto storage class.

Syntax:

register datatype var_name1 [= value];

 Example

register int rollnumber;
register char c;

C ++ code to demonstrate register storage class concept:

#include <iostream>
using namespace std;
void registerStorageClass()
{
cout << "Demonstrating the register class concepts \n ";
// register variable declaration
register char a = 'C';
// printing the register variable 'a'
cout << "Value of the variable 'a'"
<< " declared as register: " << a;
}
int main()
{ registerStorageClass();
return 0;
}

Output:

register storage class

5. External Storage Class

Simple and easy storage class that defines that variables are defined not in the same block where it is used. As it assigns a variable reference to a global variable which is usually declared outside the code. As variables of this class are “ Global Variables”.

Syntax:

extern datatype var_name1;

Example

extern float var2;

C ++ code to demonstrate extern storage class concept

#include <iostream>
using namespace std;
int i;
void externStorageClass()
{
cout << "Demonstrating the extern storage class concept \n";
extern int i;
cout << "The Value of the variable 'i'"
<< "declared, as extern: " << i << "\n";
i = 85;
cout << "Modified value of the variable 'i'" << " declared as extern: \n" << i;
}
int main()
{
// To implement extern Storage Class
externStorageClass();
return 0;
}

Output:

External Storage

Conclusion

The storage class concept is very much useful in C++  programming language because a user can define the visibility of every single variable in the program. Apart from that, a user can decide the life-time of variables using these 5 types of storage classes depending on the requirement in the code.

Recommended Articles

This has been a guide to Storage Class in C++. Here we discuss the basic concepts and different types of storage class in c++ with syntax and examples. You may also have a look at the following articles to learn more –

  1. Features of C++
  2. Static Keyword in C
  3. C++ Data Types
  4. C++ Array Functions

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

1 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - C++ Training (4 Courses, 3 Projects, 4 Quizzes) Learn More