C++程序结构.1
一:程序的存储类型:
(1):extern:外部存储:
1:extern int n:他表示变量n不在本文件中分配内存,而在程序的其他文件中分配内存,分配内存(即变量的定义).
2:默认的函数声明或定义总是extern的。
extern void hanshu();
extern void hanshu2();
3:带extern的变量或函数表示的是声明而不是定义.同一个变量不能多次定义.//源文件.cpp
#include <iostream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void hanshu(); //函数的声明一般在源文件的开始位置。 void hanshu2(); /*等价于: extern void hanshu(); extern void hanshu2(); */ int n = 0; void hanshu() //定义函数 {
hanshu2(); } int main(int argc, char** argv) { n = 3; cout<<"调用函数前n="<<n<<endl; //输出的是3; hanshu(); cout<<"调用hanshu()函数后n="<<n<<endl; //输出的是8 return 0; }
//extern.cpp
#include <iostream> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ extern int n; //这里不是定义n而是告诉编译器,n这个变量是外部的.他的内存空间在外部。 void hanshu2() //定义函数 { n = 8; } /* extern:外部存储: 1:extern int n:他表示变量n不在本文件中分配内存,而在程序的其他文件中分配内存,分配内存(即变量的定义). 2:默认的函数声明或定义总是extern的。 extern void hanshu(); extern void hanshu2(); 3:带extern的变量或函数表示的是声明而不是定义.同一个变量不能多次定义. */
(2):静态存储类型:
1:静态全局变量:在全局变量前加一个static,使该变量只在这个源文件中可以,称之为全局静态变量。
//源文件.cpp #include <iostream> using namespace std; int n = 0; //全局变量. void fn(); //声明函数 int main() { n = 20; cout<<n<<endl; //20 fn(); cout<<"调用函数fn()后n="<<endl; //20 return 0; }
//静态类型 #include <iostream> using namespace std; static int n; //默认初始化为0,这里指明了n不是外部的变量,他的空间在本文件中分配,不为外部使用。 void fn() { n++; cout<<"在非源文件中定义了函数fn()输出n="<<endl;//输出的是1而不是21说明两个变量是在不同的存储空间中。 }
2:静态函数:在文件作用域下声明的inline函数默认为static存储类型,在文件作用域下声明的const的常量(常量一旦定义就不能在修改。)也默认为static存储类型.如果加上extern则为外部变量.
二:C++的作用域范围分为局部作域(块作用域)、函数作用域、函数原型作用域、文件作用域和类作用域。
(1):局部作用域:当标识符的声明出现在由一对大括号括起来的一段程序(块)内时,该标识符的作用域从声明点开始,到块结束为止。
//源文件.cpp #include <iostream> using namespace std; void hanshu(); //函数的声明 void hanshu2(); int f(); void fn(int n); int mian() { hanshu(); hanshu2(); }
//局部作用域.cpp
#include <iostream> using namespace std; int f() { return 1; } void hanshu() { if(int i = 10) //i的作用域从此if()括号内定义开始 i = 2 * i; else i = 100; //i的作用域到此结束. //cout<<i<<endl; //报错 i 无定义 } void hanshu2() { switch(int C = f()) //C作用域的开始处. { case 1: cout<<C<<endl; } //i的作用域到此结束. // cout<<i<<endl; //报错 i 无定义 } void fn(int n) { if(n>5) int i = n; //整型i的作用域从此开始,到此结束 else double i = n; //double型i 的作用域从此开始,到此结束 //cout<<i<<endl; //报错i 无定义 }
三:函数作用域:
1:文件作用域:静态全局变量,静态函数都是文件作用域的,所以,文件作用域即为静态全局的.例如cout和cin都是在头文件iostream.h的头文件作用域中声明的标识符,这两个标识符的作用域延伸到嵌入iostream.h的源文件中。
main.cpp
#include <iostream> using namespace std; /*文件作用域是在所有函数定义之外说明的,其作用域从说明点开始,一直延伸到源文件结束*/ /* run this program using the console pauser or add your own getch, system("pause") or input loop */ void fn(int a,int b); //函数原型声明 int number; static int value; //静态全局变量;是文件作用域 int main(int argc, char** argv) { int a = 1; int b = 0; switch(a) //【重点注意】 { // int b = 0; 在switch中定义的变量b无法到达case1中. case 1: b++; } fn(2,3); return 0; }
函数原型作用域.cpp
#include <iostream> using namespace std; /* */ void fn(int a,int b) //函数原型定义. { a = 20; cout<<a<<endl; }
四:生命期:也叫生存期,生命期与存储区域密切相关,存储区域主要有代码区,数据区,栈区,堆区。对应的生命周期为静态生命期,局部生命期和动态生命期。
main.cpp
#include <iostream> using namespace std; void fn(); /*文件作用域是在所有函数定义之外说明的,其作用域从说明点开始,一直延伸到源文件结束*/ /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int n = 20; int main(int argc, char** argv) { double m = 3.8;//局部变量,不能在fn函数作用域块中使用 cout<<"n = "<<n<<endl; fn(); return 0; }
fn.cpp
#include <iostream> using namespace std; /* */ void fn() //函数原型定义. { //被调用函数不能享有使用调用函数中数据的权力。 extern int n; //cout<<"m = "<<m<<endl; cout<<"n = "<<n<<endl; }