Scope Demonstration

The program of Fig. 6.12 demonstrates scoping issues with global variables, automatic local variables and static local variables. Line 10 declares and initializes global variable x to 1. This global variable is hidden in any block (or function) that declares a variable named x. In main, line 14 displays the value of global variable x. Line 16 declares a local variable x and initializes it to 5. Line 18 outputs this variable to show that the global x is hidden in main. Next, lines 20–24 define a new block in main in which another local variable x is initialized to 7 (line 21). Line 23 outputs this variable to show that it hides x in the outer block of main as well as the global x. When the block exits, the variable x with value 7 is destroyed automatically. Next, line 26 outputs the local variable x in the outer block of main to show that it’s no longer hidden.


 1   // Fig. 6.12: fig06_12.cpp
 2   // Scoping example.
 3   #include <iostream>
 4   using namespace std;
 5
 6   void useLocal(); // function prototype
 7   void useStaticLocal(); // function prototype
 8   void useGlobal(); // function prototype
 9
10   int x = 1; // global variable
11
12   int main()
13   {
14      cout << "global x in main is " << x << endl;
15
16      int x = 5; // local variable to main
17
18      cout << "local x in main's outer scope is " << x << endl;
19
20      { // start new scope                                        
21         int x = 7; // hides both x in outer scope and global x   
22                                                                  
23         cout << "local x in main's inner scope is " << x << endl;
24      } // end new scope                                          
25
26      cout << "local x in main's outer scope is " << x << endl;
27
28      useLocal(); // useLocal has local x
29      useStaticLocal(); // useStaticLocal has static local x
30      useGlobal(); // useGlobal uses global x
31      useLocal(); // useLocal reinitializes its local x
32      useStaticLocal(); // static local x retains its prior value
33      useGlobal(); // global x also retains its prior value
34
35      cout << " local x in main is " << x << endl;
36   } // end main
37
38   // useLocal reinitializes local variable x during each call
39   void useLocal()
40   {
41      int x = 25; // initialized each time useLocal is called
42
43      cout << " local x is " << x << " on entering useLocal" << endl;
44      ++x;
45      cout << "local x is " << x << " on exiting useLocal" << endl;
46   } // end function useLocal
47
48   // useStaticLocal initializes static local variable x only the
49   // first time the function is called; value of x is saved
50   // between calls to this function
51   void useStaticLocal()
52   {
53      static int x = 50; // initialized first time useStaticLocal is called
54
55      cout << " local static x is " << x << " on entering useStaticLocal"
56         << endl;
57      ++x;
58      cout << "local static x is " << x << " on exiting useStaticLocal"
59         << endl;
60   } // end function useStaticLocal
61
62   // useGlobal modifies global variable x during each call
63   void useGlobal()
64   {
65      cout << " global x is " << x << " on entering useGlobal" << endl;
66      x *= 10;
67      cout << "global x is " << x << " on exiting useGlobal" << endl;
68   } // end function useGlobal


global x in main is 1
local x in main's outer scope is 5
local x in main's inner scope is 7
local x in main's outer scope is 5

local x is 25 on entering useLocal
local x is 26 on exiting useLocal

local static x is 50 on entering useStaticLocal
local static x is 51 on exiting useStaticLocal

global x is 1 on entering useGlobal
global x is 10 on exiting useGlobal

local x is 25 on entering useLocal
local x is 26 on exiting useLocal

local static x is 51 on entering useStaticLocal
local static x is 52 on exiting useStaticLocal

global x is 10 on entering useGlobal
global x is 100 on exiting useGlobal

local x in main is 5


Fig. 6.12. Scoping example.

To demonstrate other scopes, the program defines three functions, each of which takes no arguments and returns nothing. Function useLocal (lines 39–46) declares automatic variable x (line 41) and initializes it to 25. When the program calls useLocal, the function prints the variable, increments it and prints it again before the function returns program control to its caller. Each time the program calls this function, the function recreates automatic variable x and reinitializes it to 25.

Function useStaticLocal (lines 51–60) declares static variable x and initializes it to 50. Local variables declared as static retain their values even when they’re out of scope (i.e., the function in which they’re declared is not executing). When the program calls useStaticLocal, the function prints x, increments it and prints it again before the function returns program control to its caller. In the next call to this function, static local variable x contains the value 51. The initialization in line 53 occurs only once—the first time useStaticLocal is called.

Function useGlobal (lines 63–68) does not declare any variables. Therefore, when it refers to variable x, the global x (line 10, preceding main) is used. When the program calls useGlobal, the function prints the global variable x, multiplies it by 10 and prints it again before the function returns program control to its caller. The next time the program calls useGlobal, the global variable has its modified value, 10. After executing functions useLocal, useStaticLocal and useGlobal twice each, the program prints the local variable x in main again to show that none of the function calls modified the value of x in main, because the functions all referred to variables in other scopes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.64.241