forked from pdeitel/CPlusPlus20ForProgrammers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig05_06.cpp
executable file
·79 lines (63 loc) · 3.02 KB
/
fig05_06.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// fig05_06.cpp
// Scoping example.
#include <iostream>
using namespace std;
void useLocal(); // function prototype
void useStaticLocal(); // function prototype
void useGlobal(); // function prototype
int x{1}; // global variable
int main() {
cout << "global x in main is " << x << '\n';
const int x{5}; // local variable to main
cout << "local x in main's outer scope is " << x << '\n';
{ // block starts a new scope
const int x{7}; // hides both x in outer scope and global x
cout << "local x in main's inner scope is " << x << '\n';
}
cout << "local x in main's outer scope is " << x << '\n';
useLocal(); // useLocal has local x
useStaticLocal(); // useStaticLocal has static local x
useGlobal(); // useGlobal uses global x
useLocal(); // useLocal reinitializes its local x
useStaticLocal(); // static local x retains its prior value
useGlobal(); // global x also retains its prior value
cout << "\nlocal x in main is " << x << '\n';
}
// useLocal reinitializes local variable x during each call
void useLocal() {
int x{25}; // initialized each time useLocal is called
cout << "\nlocal x is " << x << " on entering useLocal\n";
++x;
cout << "local x is " << x << " on exiting useLocal\n";
}
// useStaticLocal initializes static local variable x only the
// first time the function is called; value of x is saved
// between calls to this function
void useStaticLocal() {
static int x{50}; // initialized first time useStaticLocal is called
cout << "\nlocal static x is " << x
<< " on entering useStaticLocal\n";
++x;
cout << "local static x is " << x <<
" on exiting useStaticLocal\n";
}
// useGlobal modifies global variable x during each call
void useGlobal() {
cout << "\nglobal x is " << x << " on entering useGlobal\n";
x *= 10;
cout << "global x is " << x << " on exiting useGlobal\n";
}
/**************************************************************************
* (C) Copyright 1992-2022 by Deitel & Associates, Inc. and *
* Pearson Education, Inc. All Rights Reserved. *
* *
* DISCLAIMER: The authors and publisher of this book have used their *
* best efforts in preparing the book. These efforts include the *
* development, research, and testing of the theories and programs *
* to determine their effectiveness. The authors and publisher make *
* no warranty of any kind, expressed or implied, with regard to these *
* programs or to the documentation contained in these books. The authors *
* and publisher shall not be liable in any event for incidental or *
* consequential damages in connection with, or arising out of, the *
* furnishing, performance, or use of these programs. *
**************************************************************************/