Lecture 09 OF C++
-
Upload
mehmood-khan-marwat -
Category
Documents
-
view
217 -
download
0
Transcript of Lecture 09 OF C++
-
7/23/2019 Lecture 09 OF C++
1/22
1
LECTURE 9
-
7/23/2019 Lecture 09 OF C++
2/22
2
Local Variables
Variables declared in a function: Are localto that function, they cannot be used
from outside the function
Have the function as their scope
Variables declared in the main part of aprogram: Are local to the main part of the program, they
cannot be used from outside the main part Have the main part as their scope
-
7/23/2019 Lecture 09 OF C++
3/22
3
Global Constants
Global Named Constant Available to more than one function as well as the
main part of the program eclared outside any function body
eclared outside the main function body eclared before any function that uses it
!"ample: const double #$ % &'1(1)*+ double volumedouble-+
int main- ./0 #$ is available to the main function and to function
volume
-
7/23/2019 Lecture 09 OF C++
4/22
4
Global Variables Global Variable
2arely used when more than one function must use acommon variable eclared 3ust li4e a global constant e"cept const
is not used Generally ma4e programs more difficult to
understand and maintain
-
7/23/2019 Lecture 09 OF C++
5/22
5
Storage Classes Attributes of variable includes
name, type, si5e and value
!ach identifier in a program also has other attributes
including Storage class
How long variable e"ists in memory
Scope 6here variable can be referenced in program
Linkage 7or multiple sourcefile program see Ch' 8-, which files can use it
-
7/23/2019 Lecture 09 OF C++
6/22
6
Storage Classes C99 provides five storage class specifiers
Auto
Register
Extern
Mutable Static
An identifiers storage class specifier helps determine its ;torage class, scope and lin4age
-
7/23/2019 Lecture 09 OF C++
7/22
7
Automatic Storage Classes =ey words autoand registerare used to declare variables of
automatic storage class
Variable created when program enters its bloc4
Variable e"it while the bloc4 is active
Variable destroyed when program leaves bloc4
>nly local variables of functions can be automatic
Automatic by default
4eyword autoe"plicitly declares automatic auto double x, y; /*indicates x and y are local
variables of automatic storage class, exist only in
the body of function in which definition appear*/
-
7/23/2019 Lecture 09 OF C++
8/22
8
Automatic storage class
registerkeyword Hint to place variable in highspeed register
Good for oftenused items loop counters-
>ften unnecessary, compiler optimi5es
;pecify either registeror auto, not both
register int counter = 1; /*integer variable
counter be placed in one of the computers
registers, counter is initialized to 1*/
-
7/23/2019 Lecture 09 OF C++
9/22
9
Static storage class =ey words externand staticare used to declare identifiers for
variables and functions of static storage class
Variables e"ist for entire program 7or functions, name e"ists for entire program
?ay not be accessible, scope rules still apply more later-
statickeyword @ocal variables in function declared with 4eyword static
=eeps value between function calls
>nly 4nown in own function
;tatic local variables retain their values when function is e"ited
static int count = 1; /* declare local variable count to be
static and initialized to 1
@ocal variable $nitiali5ed to 0by default
-
7/23/2019 Lecture 09 OF C++
10/22
10
Static storage class
externkeyword !"ternal identifier
efault for global variablesfunctions names
Global: variable declaration outside of a function bloc4
=nown in any function that comes after it
Global variables retain value through out the e"ecution ofprogram
Generally the use of global variable should be avoided e"ceptin certain situations with uniBue performance reBuirement
-
7/23/2019 Lecture 09 OF C++
11/22
11
Scope Rules
Scope #ortion of program where identifier can be
used
;copes for an identifier are 7unction scope
7ile scope
loc4 scope
7unction prototype scope
Class scope discuss later, chapter 8-
namespace scope discuss later, chapter D1-
-
7/23/2019 Lecture 09 OF C++
12/22
12
Scope Rules
7ile scope efined outside a function, 4nown in all functions
Global variables, function definitions and prototypes allhave file scope
7unction scope Can only be referenced inside function in which they
appear, can not be referenced outside function body
>nly labels are the identifiers with function scope labels are identifiers with a colon e'g' case: as used in switch-
-
7/23/2019 Lecture 09 OF C++
13/22
13
Scope Rules
loc4 scope $dentifiers declared inside the bloc4
egins at declaration, ends at right brace } of thebloc4 Can only be referenced in this range
@ocal variables, function parameters
$n case of nested bloc4s and identifiers in inner andouter bloc4 have same name $dentifier in the outer bloc4 is hidden until the inner bloc4
terminates
staticvariables still have bloc4 scope ;torage duration does not effect the scope of bloc4
-
7/23/2019 Lecture 09 OF C++
14/22
14
Scope Rules
7unctionprototype scope
>nly identifiers with this scope are those used in#arameter list of prototype
Names in function prototype optional, only type reBuired Compiler ignores the name if used in parameter list of
functionprototype
$n a single prototype, name can be used once
$dentifier used in function prototype can be reusedelsewhere in the program
-
7/23/2019 Lecture 09 OF C++
15/22
15
A scoping example program
Ne"t we are going to discuss a program that willdemonstrates the scoping issues with
Global variables
Automatic local variables
;tatic local variables
-
7/23/2019 Lecture 09 OF C++
16/22
16
A scoping example program
Program description
Globalvariable xis declared and initiali5ed to 1, hidden in anybloc4 or function with a variable named xdeclared in it
$n main local variable xis declared and initiali5ed to andprinted to show that global xis hidden in main E%) is printed to show that global " is hidden in main
New bloc4 defined in main with localvariable xinitiali5ed to !,printed to show it hides xin outer bloc4 of main, "%Fautomatically destroyed when bloc4 is e"ited #rinted to show it hides " in outer bloc4 of main
-
7/23/2019 Lecture 09 OF C++
17/22
17
A scoping example program 7unctionuseLocaldefines automaticvariable x " #' calling
useLocal prints x$increment and then again print x !ach time function is called auto variable is recreated and
initiali5ed to #
%seStaticLocaldeclare staticvariable x " 0local variabledeclared static retain their value even when out of scope'calling %seStaticLocalprints x$increment and then againprint x
useGlobaldoes not declare any variable so it uses the globalx' Calling &unctionprints globalvariable, multiply it by 10,printed again before e"iting function ne"t time it is called, global variable has its modified value i'e' 10
-
7/23/2019 Lecture 09 OF C++
18/22
18
A scoping example program
7inally the program prints the local variable " in main again toshow
None of the function calls modified the value of x
ecause the function referred to variables in other scopes
-
7/23/2019 Lecture 09 OF C++
19/22
19
C++ code1 7ig' &'1D: fig&1D'cpp2 A scoping e"ample'3 IincludeJiostreamK4
5 usingstd::cout+6 usingstd::endl+7
8 voiduse@ocal void-+ function prototype9 voiduse;tatic@ocal void-+ function prototype10 voiduseGlobal void-+ function prototype
1112 int" % 1+ global variable13
14 intmain-15 .16 int" % )+ local variable to main17
18 cout JJ Llocal " in mainMs outer scope is LJJ " JJ endl+19
20 . start new scope21
22 int" % F+23
24 cout JJ Llocal " in mainMs inner scope is LJJ " JJ endl+25
26 0 end new scope27
28 cout JJLlocal " in mainMs outer scope is LJJ " JJ endl+29
Declared outside of function;
global variable with file
scope.Local variable with functionscope.
Create a new block, giving x
block scope. When the block
ends, this xis destroyed.
-
7/23/2019 Lecture 09 OF C++
20/22
20
C++ code30 use@ocal-+ use@ocal has local "
31 use;tatic@ocal-+ use;tatic@ocal has static local "
32 useGlobal-+ useGlobal uses global "
33 use@ocal-+ use@ocal reinitiali5es its local "
34 use;tatic@ocal-+ static local " retains its prior value
35 useGlobal-+ global " also retains its value
36
37 cout JJ Lnlocal " in main is LJJ " JJ endl+
38
39 return+ indicates successful termination40
41 0 end main
42
43 use@ocal reinitiali5es local variable " during each call
44 voiduse@ocal void-
45 .
46 int" % D)+ initiali5ed each time use@ocal is called
47
48cout JJ endl JJ Llocal " is L JJ "
49 JJ L on entering use@ocalLJJ endl+
50 99"+
51 cout JJ Llocal " is LJJ "
52 JJ L on e"iting use@ocalLJJ endl+
53
54 0 end function use@ocal
55
Automatic variable local
variable of function!. "his isdestroyed when the function
e#its, and reinitiali$ed when
the function begins.
-
7/23/2019 Lecture 09 OF C++
21/22
21
C++ code56 use;tatic@ocal initiali5es static local variable " only the57 first time the function is called+ value of " is saved58 between calls to this function59 voiduse;tatic@ocal void-
60 .61 initiali5ed only first time use;tatic@ocal is called62 static int" % )+63
64 cout JJ endl JJ Llocal static " is L JJ "65 JJ L on entering use;tatic@ocalLJJ endl+
66 99"+67 cout JJ Llocal static " is LJJ "68 JJ L on e"iting use;tatic@ocalLJJ endl+69
70 0 end function use;tatic@ocal
71
72 useGlobal modifies global variable " during each call73 voiduseGlobal void-74 .75 cout JJ endl JJ Lglobal " is L JJ "
76 JJ L on entering useGlobalL JJ endl+77 " O% 1+
78 cout JJ Lglobal " is LJJ "79 JJ L on e"iting useGlobalL JJ endl+80
81 0 end function useGlobal
%tatic local variable of
function; it is initiali$ed only
once, and retains its value
between function calls.
-
7/23/2019 Lecture 09 OF C++
22/22
22
Output
Compare this output to the description of the programon slides D and D1