Global variable in C++ by R4R Team

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();
}


output-

Local variable is: 20
Global variable is: 10




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!