全局变量也称外部变量,是编程中的一种术语,对象函数是在外部定义变量,也可以在程序任何地方进行创建,当然也可以是程序和对象进行引用。
#include
using namesapce std;
int a=3;
int main()
{}
#incldue <iostream>
using namespace std;
static int a=3;
int main()
{}
#incldue <iostream>
using namespace std;
#define PI 3.1415962
int main().{}
::变量名
- 1
#include
using namespace std;
int a = 10;
int main()
{
int a = 3;
cout << "局部变量a的值是:" << a << endl;
::a;
cout << "全局变量a的值是:" << ::a << endl;
int c;
c=(a += a);
cout << "局部变量a的和为:" << c << endl;
int d;
d = (a += ::a);
cout << "改变后的局部变量a(即做了加法后)与全局变量a的和为:" << d << endl;
return 0;
}

当全局变量与局部变量同名时:在定义局部变量的子程序内,局部变量起作用:在其它地方全局变量起作用.
#include
using namespace std;
int a = 0;
void fun1()
{
int b = 3; //定义不同名局部变量
a += 5+b;
}
void fun2()
{
int a = 0; //定义同名局部变量.
a += 10;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}

#include
using namespace std;
int a = 0;
void fun1()
{
a += 5;
}
void fun2()
{
int a = 0; //定义同名局部变量.
int b = 3; //定义不同名局部变量
a += 10+b;
}
int main()
{
cout << a << endl;
fun1();
cout << a << endl;
fun2();
cout << a << endl;
return 0;
}


完结!!!!! 如有不解,可私聊