Storage Classes in C++ Programming
来源:互联网 发布:cs1.5优化教程视频 编辑:程序博客网 时间:2024/04/30 14:31
原文链接:Storage Classes in C++
Storage Classes in C++ Programming
Storage class of a variable defines the lifetime and visibility of a variable. Lifetime means the duration till which the variable remains active and visibility defines in which module of the program the variable is accessible. There are five types of storage classes in C++. They are:
- Automatic
- External
- Static
- Register
- Mutable
1. Automatic Storage Class
Automatic storage class assigns a variable to its default storage type. auto keyword is used to declare automatic variables. However, if a variable is declared without any keyword inside a function, it is automatic by default. This variable isvisible only within the function it is declared and its lifetime is same as the lifetime of the function as well. Once the execution of function is finished, the variable is destroyed.
Syntax of Automatic Storage Class Declaration
datatype var_name1 [= value];orauto datatype var_name1 [= value];
Example of Automatic Storage Class
auto int x;float y = 5.67;
2. External Storage Class
External storage class assigns variable a reference to a global variable declared outside the given program. extern keyword is used to declare external variables. They are visible throughout the program and its lifetime is same as the lifetime of the program where it is declared. This visible to all the functions present in the program.
Syntax of External Storage Class Declaration
extern datatype var_name1;
For example,
extern float var1;
Example of External Storage Class
Example 1: C++ program to create and use external storage.
File: sub.cppint test=100; // assigning value to testvoid multiply(int n){ test=test*n;}
File: main.cpp#include<iostream>#include "sub.cpp" // includes the content of sub.cppusing namespace std;extern int test; // declaring testint main(){ cout<<test<<endl; multiply(5); cout<<test<<endl; return 0;}
A variable test is declared as external in main.cpp. It is a global variable and it is assigned to 100in sub.cpp. It can be accessed in both files. The function multiply() multiplies the value of test with the parameter passed to it while invoking it. The program performs the multiplication and changes the global variable test to 500.
Note: Run the main.cpp program
Output
100500
3. Static Storage Class
Static storage class ensures a variable has the visibility mode of a local variable but lifetime of an external variable. It can be used only within the function where it is declared but destroyed only after the program execution has finished. When a function is called, the variable defined as static inside the function retains its previous value and operates on it. This is mostly used to save values in a recursive function.
Syntax of Static Storage Class Declaration
static datatype var_name1 [= value];
For example,
static int x = 101;static float sum;
4. Register Storage Class
Register storage assigns a variable's storage in the CPU registers rather than primary memory. It has its lifetime and visibility same as automatic variable. The purpose of creating register variable is to increase access speed and makes program run faster. If there is no space available in register, these variables are stored in main memory and act similar to variables of automatic storage class. So only those variables which requires fast access should be made register.
Syntax of Register Storage Class Declaration
register datatype var_name1 [= value];
For example,
register int id;register char a;
Example of Storage Class
Example 2: C++ program to create automatic, global, static and register variables.
#include<iostream>using namespace std;int g; //global variable, initially holds 0void test_function(){ static int s; //static variable, initially holds 0 register int r; //register variable r=5; s=s+r*2; cout<<"Inside test_function"<<endl; cout<<"g = "<<g<<endl; cout<<"s = "<<s<<endl; cout<<"r = "<<r<<endl;}int main(){ int a; //automatic variable g=25; a=17; test_function(); cout<<"Inside main"<<endl; cout<<"a = "<<a<<endl; cout<<"g = "<<g<<endl; test_function(); return 0;}
In the above program, g is a global variable, s is static, r is register and a is automatic variable. We have defined two function, first is main() and another is test_function(). Since g is global variable, it can be used in both function. Variables r and s are declared inside test_function() so can only be used inside that function. However, s being static isn't destroyed until the program ends. When test_function() is called for the first time, r is initialized to 5 and the value of s is 10 which is calculated from the statement,
s=s+r*2;
After the termination of test_function(), r is destroyed but s still holds 10. When it is called second time, r is created and initialized to 5 again. Now, the value of s becomes 20 since s initially held 10. Variable a is declared inside main() and can only be used inside main().
Output
Inside test_functiong = 25s = 10r = 5Inside maina = 17g = 25Inside test_functiong = 25s = 20r = 5
5. Mutable Storage Class
In C++, a class object can be kept constant using keyword const. This doesn't allow the data members of the class object to be modified during program execution. But, there are cases when some data members of this constant object must be changed. For example, during a bank transfer, a money transaction has to be locked such that no information could be changed but even then, its state has be changed from - started to processing to completed. In those cases, we can make these variables modifiable using a mutable storage class.
Syntax for Mutable Storage Class Declaration
mutable datatype var_name1;
For example,
mutable int x;mutable char y;
Example of Mutable Storage Class
Example 3: C++ program to create mutable variable.
#include<iostream>using namespace std;class test{ mutable int a; int b; public: test(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 test x(2,3); cout<<"Initial value"<<endl; x.display(); x.square_a(); cout<<"Final value"<<endl; x.display(); return 0;}
A class test is defined in the program. It consists of a mutable data member a. A constant object xof class test is created and the value of data members are initialized using user-defined constructor. Since, b is a normal data member, its value can't be changed after initialization. However a being mutable, its value can be changed which is done by invoking square_a() method. display() method is used to display the value the data members.
Output
Initial valuea = 2b = 3Final valuea = 4b = 3
- Storage Classes in C++ Programming
- C Storage Classes
- C Storage Classes || C 语言存储类别
- C Storage Classes-C语言的存储类型
- Programming experience in C
- Substring in c programming
- Practical Programming in C
- Programming SQLite3 in C
- Programming in scala学习笔记(三)Classes and Objects
- PLI Storage Classes
- 读书笔记《Programming in C++》 开篇
- 读书笔记《Programming in C++》之一
- 混沌 IN C++::Generic Programming
- Notes on Programming in C
- Notes on Programming in C
- Notes on Programming in C
- Static variables in c programming
- C/C++ programming in Emacs
- Hashtable与HashMap的区别
- codeforces-C. Journey(广搜+记录路径)
- 将你的Laravel应用部署到Heroku上
- ListView点击item底部弹出popupWindow删除、修改、取消选择框
- 嵌入式系统学习(三)-S5P4418 芯片存储空间分布说明
- Storage Classes in C++ Programming
- 【C/C++语言】指针常量与常量指针的区别
- 【POJ1011】Sticks-DFS+调整法剪枝
- 【myfocus】一款好用的焦点图轮播插件
- 62.Search in Rotated Sorted Array-搜索旋转排序数组(中等题)
- Android之SQLite数据库
- 开发中乱码问题
- 图
- Android版本下载以及切换之 git使用