Download as pdf or txt
Download as pdf or txt
You are on page 1of 168

Scopes in C++

Dr Ayesha Zeb
Department of Mechatronics Engineering
NUST, CEME.
Lecture Contents
• Variables and storage classes
• Scope and lifetime of
• Local variables
• Global variables
• Static variables
Learning Objectives
• To understand the concept of variable scope and storage class.
• To understand the concept of variable lifetime and scope for local,
global and static variables.
Variables and Storage Classes
• The storage class of a variable determines which parts of the
program can access it (scope)and how long it stays in
existence
(lifetime)
• Types of variables
• Automatic or Local
• Global or External Variable
• Static
Scope and Life Time of Variable
• Life Time
• The time b/w creation and destruction of variable is called the life time or
duration. Lifetime of a variable is limited to save memory.
• Scope
• Scope refers to blocks of code within which variables are visible. Scope limits
visibility to improve organization and modularity of the program.
Scope
• The life-time of variables exists within the variable’s scope.
• After declaration, a variable can be referenced anywhere within its scope.
• Every set of braces has its own scope, and can contain local variables.
• The moment the set of braces in which a variable was declared ends, the variable
goes out of scope, i.e. it can no longer be referenced as an identifier.
Scope
• The program erases variables that have gone out of scope from memory, i.e. their
life-time ends.
• The scope of arguments to a function is the entire function body.
• No two variables may share the same name within a scope.
Scope
• Example: A variable declared in the first line of a function can be used anywhere
in the function, but nowhere outside of it.
• The moment the function exits, the variable ceases to exist in memory, i.e. its
scope and lifetime end.
Automatic or Local Variables
• Variables defined within the function body (or a set of braces) are
called automatic or local variables.
• Properties
• It is most commonly used.
• It is not created until the function in which it is defined is called.
Automatic or Local Variables
• Life Time
• When control is returned to the calling program, these variables
are lost. Life time is equal to the life of function.
• Scope
• Automatic variable are only visible with in function, they can only be accessed
within the function in which they are created.
Example (Local Variables)
#include <math.h> void squareroot( float a)
{
void squareroot(float); float y;
y = y+sqrt(a);
int main(void) cout <<"The answer = " <<
{ y <<endl;
float x; }
x = 4.0;
squareroot(x); //function
call
}
External or Global Variables
• Variable defined outside the function. Normally it is declared before main
function.
• Properties
• Every function can access global variables.
• Thus, any function can change the values of global variables.
• This creates organizational problems
Global Variables
• Global variables should generally be avoided unless necessary.
• It is a good practice to use ::globalvariable_name for using a global
variable.
• eg, if ‘a’ is the global variable, write ‘::a’ where-ever ‘a’ is used in
the code
Global Variables
• Life Time
• is equal to the life of program.
• Scope
• It is visible to all the function in a program.
Example (Global Variables)
float sum(); // prototype
float first, second;
int main(void)
{
float first, second;
::first = 23.45;
::second = 87.555;
cout <<"sum = “ << sum();
return 0;
}
float sum()
{
return ::first + ::second;}
Static Local Variables
• Static variable remembers and maintains its value even after its scope
ends.
• Static Local variables are used when it is necessary for a function to
remember a value between call to other functions.
• For example, variable count used in a func
Static Local Variables
• Scope
• Static variable is visible only inside the function in which it is defined.
• It has the same scope as local variables.
• Life time
• Life of a static variable is the same as that of a global variable, i.e. it remains
in existence for the life of the program.
Code Action Start program
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for (int i=0; i<10; i++)
function();
}

void function( void )


{
static int count = 0; // initialized first
time only Output
count++;
cout << count;
}
Code Action Start scope of main function
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for (int i=0; i<10; i++)
function();
}

void function( void )


{
static int count = 0; // initialized first
time only Output
count++;
cout << count;
}
Code Action output
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for (int i=0; i<10; i++)
function();
}

void function( void )


{
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action for loop initialization statement
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0 ; i<10; i++)
function();
}

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action check condition of for loop
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++) Condition true
function();
}

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action function call
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action start scope of new function
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action initialize static variable as 1
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 0

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action increment count
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 1

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :
}
Code Action display count
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 1

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 1
}
Code Action end function scope (but lifetime
void function(void); of static variable still exists)

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 1

void function( void )


{ i 0
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 1
}
Code Action increment i
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++ )
function();
}
count 1

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 1
}
Code Action check condition
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++) Condition true
function();
}
count 1

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 1
}
Code Action call function
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 1

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 1
}
Code Action start scope of new function
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 1

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :1
}
Code Action increment count
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 2

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called :1
}
Code Action display count
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 2

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12
}
Code Action end function scope (but lifetime
void function(void); of static variable still exists)

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 2

void function( void )


{ i 1
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12
}
• At i = 9, function will be called for the last time
Code Action end function scope (but lifetime
void function(void); of static variable still exists)

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}
count 10

void function( void )


{ i 9
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12345678910
}
Code Action increment i
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++ )
function();
}
count 10

void function( void )


{ i 10
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12345678910
}
Code Action check condition
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++) Condition false
function();
}
count 10
void function( void )
{ i 10
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12345678910
}
Code Action end scope of main
void function(void);

int main()
{
cout << "Count the number of times the Memory
function is called : ";
for ( int i=0; i<10 ; i++)
function();
}

void function( void )


{
static int count = 0; // initialized first
time only Output
count++; Count the number of times the function
cout << count; is called : 12345678910
}
Variables and Storage Classes
Variable Type Scope Lifetime
Local or Auto Within braces{} Within braces{}
Global or Entire program Entire program
External
Static Within braces Entire program
{}
Code Action Start program
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5;
cout << x << " " << y << endl;
} Output
cout << x << " " << y << endl;
}
Code Action start scope of main function
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5;
cout << x << " " << y << endl;
} Output
cout << x << " " << y << endl;
}
Code Action allocate x,y
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X y
cout << x << " " << y << endl;
} Output
cout << x << " " << y << endl;
}
Code Action x is set to 3
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X 3 y
cout << x << " " << y << endl;
} Output
cout << x << " " << y << endl;
}
Code Action y is set to 4
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
}
Code Action open scope
//scopes_example1
#include<iostream>
using namespace std;
void main()
Memory
{
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x; X 3 y 4
x = 5;
cout << x << " " << y << endl; Output
} 3 4
cout << x << " " << y << endl;
}

The inner or nested scope is opened


Code Action allocate x
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
X
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
}

A variable x is declared. This is visible only in the inner scope. It shadows the outer x.
Code Action assign x the value 5
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
X 5
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
X 5
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
5 4
}

The inner x and outer y are printed. Note that inner is upper.
Code Action close scope
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
5 4
}
Code Action output x y
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5; X 3 y 4
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
5 4
} 3 4

The outer x and outer y are printed


Code Action close scope
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5;
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
5 4
} 3 4
Code Action end program
//scopes_example1
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int x;
x = 5;
cout << x << " " << y << endl;
} Output
3 4
cout << x << " " << y << endl;
5 4
} 3 4
Code Action Start program
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action start scope of main function
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action allocate a,b,c
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a b c
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action a is set to 2
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b c
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action b is set to 3
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b 3 c
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action b is set to 3
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action output x y
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
}
Code Action open scope
//scopes_example2
#include<iostream>
using namespace std;
void main()
{ Memory
int a, b, c;
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c; a 2 b 3 c 7
a = 5;
c = 9; Output
cout << a << " " << b << " " << c << endl; 2 3 7
}
cout << a << " " << b << " " << c << endl;
}

The inner or nested scope is opened


Code Action allocate a , c
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl; a c
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
}
Code Action assign a value 5
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl; a 5 c
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
}
Code Action assign c value 9
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl; a 5 c 9
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
}
Code Action output a b c
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl; a 5 c 9
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
5 3 9
}
Code Action close scope
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
5 3 9
}
Code Action output a b c
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a 2 b 3 c 7
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
5 3 9
2 3 7
}
Code Action close scope
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
} 2 3 7
cout << a << " " << b << " " << c << endl;
5 3 9
2 3 7
}
Code Action end program
//scopes_example2
#include<iostream>
using namespace std;
void main()
{
int a, b, c; Memory
a = 2;
b = 3;
c = 7;
cout << a <<" " << b << " " << c << endl;
{
int a, c;
a = 5;
c = 9;
cout << a << " " << b << " " << c << endl; Output
}
cout << a << " " << b << " " << c << endl;
}
Code Action Start program
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action start scope of main function
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action allocate x,y
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X y
z = 1;
cout << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action x is set to 3
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y
z = 1;
cout << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action y is set to 4
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y 4
z = 1;
cout << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y 4
z = 1;
cout << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action open scope
//scopes_example3
#include<iostream>
using namespace std;
void main()
Memory
{
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ; X 3 y 4
y = 7;
z = 1; Output
cout << y << " " << z << endl; 3 4
}
cout << x << " " << y << endl;
}
The inner or nested scope is opened
Code Action allocate x
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y z
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action assign y the value 7
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action assign z the value 1
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z 1
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action output y z
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z 1
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
7 1
cout << x << " " << y << endl;
}
Code Action close scope
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
7 1
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
7 1
cout << x << " " << y << endl; 3 4
}
Z is not visible at this point and thus cannot be printed. Try cout<<x<<y<<z;
Code Action close scope
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
7 1
cout << x << " " << y << endl; 3 4
}
Code Action end program
//scopes_example3
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << y << " " << z << endl; Output
3 4
}
7 1
cout << x << " " << y << endl; 3 4
}
Code Action Start program
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action start scope of main function
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action allocate x,y
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X y
z = 1;
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action x is set to 3
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y
z = 1;
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action y is set to 4
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y 4
z = 1;
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7; X 3 y 4
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action open scope
//scopes_example4
#include<iostream>
using namespace std;
void main()
Memory
{
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ; X 3 y 4
y = 7;
z = 1; Output
cout << x << " " << y << " " << z << endl; 3 4
}
cout << x << " " << y << endl;
}
The inner or nested scope is opened
Code Action allocate x
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y z
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action assign y the value 7
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action assign z the value 1
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z 1
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
cout << x << " " << y << endl;
}
Code Action output x y z
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
y 7 z 1
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
3 7 1
cout << x << " " << y << endl;
}
No variable called x is declared in this scope, so the outer a is not shadowed. The value of outer x will be printed
Code Action close scope
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
3 7 1
cout << x << " " << y << endl;
}
Code Action output x y
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
X 3 y 4
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
3 7 1
cout << x << " " << y << endl; 3 4
}
Z is not visible at this point and thus cannot be printed. Try cout<<x<<y<<z;
Code Action close scope
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
3 7 1
cout << x << " " << y << endl; 3 4
}
Code Action end program
//scopes_example4
#include<iostream>
using namespace std;
void main()
{
Memory
int x, y;
x = 3;
y = 4;
cout << x <<" " << y << endl;
{
int y , z ;
y = 7;
z = 1;
cout << x << " " << y << " " << z << endl; Output
3 4
}
3 7 1
cout << x << " " << y << endl; 3 4
}
Deeper Nesting
• Scopes can be nested to any number of levels. In the following
program we have 3 levels.
• Note carefully that visibility is determined by what can be seen by
looking down at the frame from top of the page.
Code Action Start program
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ;
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action start scope of main function
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ;
cout << x << " " << y << " " << z << " “<< w <<
endl;
} Output
cout << x << " " << y << " " << z << endl;
}
cout << x << " " << y << endl;
}
Code Action allocate x,y and initialize x with 3
//scopes_example5 and y with 4
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ;
X 3 y 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
}
cout << x << " " << y << endl;
}
Code Action output x and y
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ;
X 3 y 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
}
Code
Action open scope
//scopes_example5
#include<iostream>
using namespace std;
void main()
{ Memory
int x = 3, y = 4;
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
X 3 y 4
int y = 0 , z = 2 , w = 9 ;
cout << x << " " << y << " " << z << " “ << w << endl;
} Output
cout << x << " " << y << " " << z << endl; 3 4
}
cout << x << " " << y << endl;
}
Code Action allocate y and z and initialize y to
//scopes_example5 7 and z to 1
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
}
Code Action output x y and z
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
}
Code Action new scope
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
}
Code Action allocate y z and w and initialize y
//scopes_example5 to 0, z to 2 and w to 9
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{ y 0 z 2 w 9
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ;
X 3 y 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl; 3 7 1
}
Code Action output x y z and w
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{ y 0 z 2 w 9
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
Code Action close scope
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
Code Action output x y and z
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl; y 7 z 1
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
3 7 1
Code Action close scope
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
3 7 1
Code Action output x and y
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ; y
X 3 4
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
3 7 1
3 4
Code Action close scope
//scopes_example5
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4; Memory
cout << x <<" " << y << endl;
{
int y = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int y = 0 , z = 2 , w = 9 ;
cout << x << " " << y << " " << z << " “<< w << endl;
}
cout << x << " " << y << " " << z << endl; Output
} 3 4
cout << x << " " << y << endl;
3 7 1
3 0 2 9
}
3 7 1
3 4
Visibility issues
• In any scope we can access all the variables declared at that scope
and all other non shadowed variables.
Code
Action Start program
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
cout << x <<" " << y << endl;
Memory
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2;
Output
cout << x << " " << y << " " << z << endl;
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code
Action start scope of main function
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
cout << x <<" " << y << endl;
Memory
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2;
Output
cout << x << " " << y << " " << z << endl;
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action allocate x,y and initialize x with 3
//scopes_example6
and y with 4
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
X 3 y 4
}
y=2; Output
cout << x << " " << y << " " << z << endl;
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action allocate w
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
X 3 y 4 w
}
y=2; Output
cout << x << " " << y << " " << z << endl;
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action output x and y
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
X 3 y 4 w
}
y=2; Output
cout << x << " " << y << " " << z << endl;
} 3 4
w=5;
cout << x << " " << y << " " << w << endl;
}
Code
//scopes_example6 Action open scope
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
X 3 y 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
Output
y=2;
3 4
cout << x << " " << y << " " << z << endl;
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action allocate x and z and initialize x to
//scopes_example6
7 and z to 1
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 1
z=2;
w= 9; y
x 3 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action output x y and z
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 1
z=2;
w= 9; y
x 3 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action new scope
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 1
z=2;
w= 9; y
x 3 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action allocate x and y and initialize x to 6
//scopes_example6
and y to 0
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ; x 6 y 0
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 1
z=2;
w= 9; y
x 3 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action store 2 in z
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ; x 6 y 0
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 4 w
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action store 9 in w
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ; x 6 y 0
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 4 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
}
Code Action output x y z and w
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ; x 6 y 0
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 4 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
}
Code Action close scope
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 4 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
}
Code Action set y to 2
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 2 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
}
Code Action set y to 2
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
x 7 z 2
z=2;
w= 9; y
x 3 2 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
} 7 2 2
Code Action close scope
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9; y
x 3 2 w 9
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
} 7 2 2
Code Action set w to 5
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9; y
x 3 2 w 5
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
} 7 2 2
Code Action output x y and w
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9; y
x 3 2 w 5
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
} 7 2 2
3 2 5
Code Action close scope and end program
//scopes_example6
#include<iostream>
using namespace std;
void main()
{
int x = 3, y = 4;
int w;
Memory
cout << x <<" " << y << endl;
{
int x = 7 , z = 1 ;
cout << x << " " << y << " " << z << endl;
{
int x= 6, y = 0 ;
z=2;
w= 9;
cout << x << " " << y << " " << z << " “<< w << endl;
}
y=2; Output
cout << x << " " << y << " " << z << endl;
3 4
}
7 4 1
w=5;
cout << x << " " << y << " " << w << endl;
6 0 2 9
} 7 2 2
3 2 5
Serial Scopes
• Scopes can occur in series i.e. one after the other. In this case there is
no nesting. For example the two inner scopes in the following
example are independent and not nested.
Code Action Start program
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
}
cout << a << " " << b << endl;
}
Code Action start scope of main function
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
}
cout << a << " " << b << endl;
}
Code Action allocate a,b and initialize a with 3
//scopes_example7 and b with 4
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
}
cout << a << " " << b << endl;
}
Code Action output a b
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl;
}
Code Action new scope
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl;
}
Code Action allocate b, c and initialize values
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
b 7 c 1
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl;
}
Code Action output a b and c
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
b 7 c 1
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl; 3 7 1
}
Code Action close scope
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl; 3 7 1
}
Code Action output a and b
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl; 3 7 1
} 3 4
Code Action new scope
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl; 3 7 1
} 3 4
Code Action allocate c,d and initialize
//scopes_example7
#include<iostream>
using namespace std;
void main()
{
int a = 3, b = 4; Memory
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
c 2 d 9
}
cout << a <<" " << b << endl;
a 3 b 4
{
int c = 2, d = 9 ;
cout << a << " " << b << " " << c << " “<< d << endl; Output
} 3 4
cout << a << " " << b << endl; 3 7 1
} 3 4
Code Action output a, b, c and d
//scopes_example7
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4;
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl; c 2 d 9
}
cout << a <<" " << b << endl; a 3 b 4
{
int c = 2, d = 9 ; Output
cout << a << " " << b << " " << c << " “<< d << endl; 3 4
}
3 7 1
3 4
cout << a << " " << b << endl;
3 4 2 9
}
Code Action close scope
//scopes_example7
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4;
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl; a 3 b 4
{
int c = 2, d = 9 ; Output
cout << a << " " << b << " " << c << " “<< d << endl; 3 4
}
3 7 1
3 4
cout << a << " " << b << endl;
3 4 2 9
}
Code Action output a,b
//scopes_example7
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4;
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl; a 3 b 4
{
int c = 2, d = 9 ; Output
cout << a << " " << b << " " << c << " “<< d << endl; 3 4
}
3 7 1
3 4
cout << a << " " << b << endl;
3 4 2 9
}
3 4
Code Action close scope
//scopes_example7
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4;
cout << a <<" " << b << endl;
{
int b = 7 , c = 1 ;
cout << a << " " << b << " " << c << endl;
}
cout << a <<" " << b << endl;
{
int c = 2, d = 9 ; Output
cout << a << " " << b << " " << c << " “<< d << endl; 3 4
}
3 7 1
3 4
cout << a << " " << b << endl;
3 4 2 9
}
3 4
Scopes and Data types
• A variable can be shadowed by a variable of different type. In the
following we have ints being shadowed by char and float.
Code Action start scope of main function
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
}
}
cout << a << " " << b << " " << c << endl;
}
Code Action allocate and initialize a b and c
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
}
}
cout << a << " " << b << " " << c << endl;
}
Code Action output a b and c
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
}
cout << a << " " << b << " " << c << endl;
}
Code Action new scope
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
}
cout << a << " " << b << " " << c << endl;
}
Code Action allocate and initialize c
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
}
cout << a << " " << b << " " << c << endl;
}
Code Action output a b and c
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl;
}
Code Action new scope
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl;
}
Code Action allocate and initialize b
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl; b 9.7
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl;
}
Code Action allocate and initialize b
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl; b 9.7
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl; 3 9.7 p
}
Code Action close scope
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p'; c 112
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl; 3 9.7 p
}
Code Action close scope
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl; 3 9.7 p
}
Code Action output a b and c
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{ a 3 b 4 c 7
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl; 3 9.7 p
3 4 7
}
Code Action scope close
//scopes_example8
#include<iostream>
using namespace std;
void main()
{ Memory
int a = 3, b = 4 , c=7;
cout << a << " " << b << " " << c << endl;
{
char c = 'p';
cout << a << " " << b << " " << c << endl;
{
float b = 9.7;
cout << a << " " << b << " " << c << endl; Output
} 3 4 7
} 3 4 p
cout << a << " " << b << " " << c << endl; 3 9.7 p
3 4 7
}

You might also like