Also See more posts : www.comsciguide.blogspot.com
Name visibility :
Named entities, such as variables, functions, and com...
Also See more posts : www.comsciguide.blogspot.com
{ // starting mark of block
int c=3; // c is also a local variable
cout...
Also See more posts : www.comsciguide.blogspot.com
Here is a set of programs for u for practice.
1.
#include <iostream>
us...
Also See more posts : www.comsciguide.blogspot.com
c();
cout << "x : " << x << 'n';
cout << "y : " << y << 'n';
return 0;
...
Also See more posts : www.comsciguide.blogspot.com
cout<<b<<endl;
}
};
void c()
{
cout<<b<<endl;
b=2;
cout<<b<<endl;
int a...
Also See more posts : www.comsciguide.blogspot.com
}
void c(int a,int b)
{
cout<<a<<endl;
cout<<b<<endl;
{
int a=10;
b=12;...
of 6

Name visibility--elobal or local

The variables that we will declare in the program have different scope depending on where we had declare the variables either in block or in function etc.In this slide i have explained it in a better way for easy to understand
Published on: Mar 3, 2016
Published in: Education      
Source: www.slideshare.net


Transcripts - Name visibility--elobal or local

  • 1. Also See more posts : www.comsciguide.blogspot.com Name visibility : Named entities, such as variables, functions, and compound types need to be declared before being used in programs. The point in the program where this declaration happens influences its visibility. An entity declared outside any block has global scope called “Global variables” , meaning that its name is valid anywhere in the code. While an entity declared within a block, in a function or as a function parameters has block scope, and is only visible within the specific block in which it is declared, but not outside it. Variables with block scope are known as “Local variables”. For example : int a; // global variable void function1 () { int b; // local variable b = 0; } void function2 () { a = 1; // a is a global variable b = 2; // b is a local variable but not visible as it is not declared in this function }  we can also call a variable as Local variable ,If we can also declare it in the block or constructor. void d() { int b=2; // b is a local variable
  • 2. Also See more posts : www.comsciguide.blogspot.com { // starting mark of block int c=3; // c is also a local variable cout<<c<<endl; } // ending mark of block } Here b can be accessed inside the block also but c can’t be accessed in function d .  If the global variable value is changed anywhere in the program (i.e. with out declaring it ) ,it's value will be changed. #include<iostream> using namespace std; int b=0; // b is a global variable void c() { cout<<"before change : "<<b<<endl; b=2; cout<<"after changed : "<<b<<endl; { int b=3; // local variable cout<<"in block : "<<b<<endl; // sets value to inner b } } int main () { c(); cout<<"in main : "<<b<<endl; } Without compiling it directly in the compiler,once u check the output by u r self. Then u will get the clear idea how the values of variables will change.  Suppose if u want two values for the same variable in the same function, then a new concept namespace is to be used.
  • 3. Also See more posts : www.comsciguide.blogspot.com Here is a set of programs for u for practice. 1. #include <iostream> using namespace std; int main () { int x = 1; int y = 2; { x = 3; int y; y = 5; cout << "inner block : n"; cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; } cout << "outer block : n"; cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; return 0; } 2. #include <iostream> using namespace std; int x=0; void c() { cout << "x : " << x << 'n'; int x=4; cout << "x : " << x << 'n'; } int main () { int x = 1; int y = 2; { x = 3; int y= 5 ; cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; }
  • 4. Also See more posts : www.comsciguide.blogspot.com c(); cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; return 0; } 3. #include <iostream> using namespace std; int x=0; void c() { cout << "x : " << x << 'n'; x=4; cout << "x : " << x << 'n'; } int main () { cout << "x : " << x << 'n'; { x = 3; int y; y = 5; cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; } c(); cout << "x : " << x << 'n'; cout << "y : " << y << 'n'; return 0; } 4. #include<iostream> using namespace std; int b=0; class s { public: s() { cout<<b<<endl; int b=1;
  • 5. Also See more posts : www.comsciguide.blogspot.com cout<<b<<endl; } }; void c() { cout<<b<<endl; b=2; cout<<b<<endl; int a=10; { cout<<a<< endl; int b=3; cout<<b<<endl; } } int main () { s d; // calling constructor b=9; c(); cout<<b<<endl; } 5. #include<iostream> using namespace std; int b=0; class s { public: s() { cout<<b<<endl; int b; b=1; cout<<b<<endl; } }; void d() { int a=15; } void e() { b=16;
  • 6. Also See more posts : www.comsciguide.blogspot.com } void c(int a,int b) { cout<<a<<endl; cout<<b<<endl; { int a=10; b=12; d(); e(); cout<<a<<endl; cout<<b<<endl; } } int main () { s d; c(6,7); cout<<b<<endl; } The answers for this programs are available in the below link :