Scope
Rules of Variables
Scope
refers to the accessibility of a variable. There are four types of scopes in
C++.
They
are: Local scope, Function scope, File
scope and Class scope.
A
scope is a region or life of the variable and broadly speaking there are three
places, where variables can be declared,
•
Inside a block which is called local variables.
•
Inside a function is called function variables.
•
Outside of all functions which is called global variables.
•
Inside a class is called class variable or data members.
•
A local variable is defined within a block.A block of code begins and ends with
curly braces { }.
•
The scope of a local variable is the block in which it is defined.
•
A local variable cannot be accessed from outside the block of its declaration.
•
A local variable is created upon entry into its block and destroyed upon exit.
//Demo to test Local Scope//
#include<iostream>s
using namespace std;
int main ( )
{
int a, b ;
a = 10;
b = 20;
if (a > b)
{
int temp; //local to this if block//
temp = a;
a = b;
b = temp;
}
cout <<"\n Descending order .... \n";
cout <<a <<"\t"<<b;
return(0);
}
Descending order ….
10 20
//Demo to test Local Scope//
#include<iostream>s
using namespace std;
int main ( )
{
int a, b ;
a = 10;
b = 20;
if (a > b)
{
int temp; //local to this if block//
temp = a;
a = b;
b = temp;
}
cout <<temp;
return(0);
}
[Error] 'temp' was not declared in
this scope
On
compilation the Program 11.28(b), the compiler prompts an error message: The
variable temp is not accessible.
Because the life time of a local variable is the life time of a block in its
state of execution. Local variables die when its block execution is completed.
•
The scope of variables declared within a function is extended to the function
block, and all sub-blocks therein.
•
The life time of a function scope variable, is the life time of the function
block. The scope of formal parameters is function scope.
//Demo to test Function Scope//
#include<iostream>
using namespace std;
void add(int x, int y)
{
int m=x+y; //'m'
declared within function add()//
cout<<"\nThe
Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
return(0);
}
//Demo to test Function Scope//
#include<iostream>
using namespace std;
void add(int x, int y)
{
int m=x+y; //'m'
declared within function add()//
cout<<"\nThe
Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
cout<<m; //'m' declared within function add()//
return(0);
}
The Sum = 30
Note : In function 'int main()':
[Error] 'm' was not declared in this
scope
•
A variable declared above all blocks and functions (including main ( ) ) has
the scope of a file. The life time of a file scope variable is the life time of
a program.
•
The file scope variable is also called as global
variable.
//Demo to test File or global Scope//
#include<iostream>
using namespace std;
int file_var=20; //Declared within File//
void add(int x, int y)
{
int m=x+y+file_var;
cout<<"\n
The Sum = "<<m;
}
int main ( )
{
int a, b ;
a = 10;
b = 20;
add(a,b);
cout<<”\nThe File Variable = “<<file_var;
return(0);
}
The Sum = 50
The File Variable =20
•
A class is a new way of creating and implementing a user defined data type.
Classes provide a method for packing together data of different types.
•
Data members are the data variables that represent the features or properties
of a class.
Note: The class scope will be
discussed later in chapter “Classes and Object”.
•
The scope operator reveals the hidden scope of a variable. The scope resolution
operator (::) is used for the
following purposes.
•
To access a Global variable when there is a Local variable with same name. An
example using Scope Resolution Operator.
//Program to show that we can access a global variable
//using scope resolution operator :: when there is a local
//variable with same name //
#include<iostream>
using namespace std;
int x=45; // Global Variable x
int main()
{
int x = 10; // Local
Variable x
cout <<
"\nValue of global x is " << ::x;
cout <<
"\nValue of local x is " << x;
return 0;
}
Value of global x is 45
Value of local x is 10
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.