What is scope of variable?
-Scope of variable is the area in the program where the variable can accessible.
-Scope of variable is depend on the declaration of the variable.
-global variable have scope on entire program.
-local variable scope may be limited to some function or some part of the program.
What is global variable?
-Global variable are those whose scope are present everywhere in the program.
-or, global variable can be accessible from anywhere in the program.
-global variable are destroy when the execution of program stop.
How make global variable?
To make a variable as a global we have a two method:
-declared variable from outside of the function.
-or, if we make a variable inside function then write "global data_type variable_name"
How we access global variable if local variable are also have same name?
program:
#include< iostream>
//global variable
int a=10;
class r4r
{
//local variable
int a=20;
void func()
{
cout<<"Local variable is: "<< a;
cout<<"Global variable is: "<< ::a;
}
};
int main()
{
r4r obj;
obj.func();
}
Local variable is: 20
Global variable is: 10