c++ programming lecture 11 functions – part iii by ghada al-mashaqbeh the hashemite university...
TRANSCRIPT
C++ ProgrammingLecture 11
Functions – Part III
ByGhada Al-MashaqbehThe Hashemite UniversityComputer Engineering Department
The Hashemite University 2
Outline
Introduction. Identifiers storage classes. Identifiers scope rules.
The Hashemite University 3
Introduction The main attributes attached with any
variable or identifier include: Name, type, size, value (as taken before). Storage class
Determines the period during which the variable exists in memory
Scope Where the identifier can be referenced in program
Linkage Where an identifier is known in multiple source
file programs.
The Hashemite University 4
Storage Classes Storage classes types:
Automatic storage. Static storage.
Five storage-class specifiers (or keywords used in C++): auto, register, extern, mutable, static.
The Hashemite University 5
Automatic Storage Variables created and destroyed within its block
(created when entering the block and destroyed when leaving this block).
Can only be used with local variables and parameters of a specified function.
Two types: auto
Default for local variables. Example:
auto float x, y; register
Tries to put variables into high-speed registers. The compiler can ignore it if no sufficient number of
registers are available. Not necessary in optimized compilers.
The Hashemite University 6
Static Storage Variables exist from the point of declaration for
entire program execution. Static variables are created and initialized once
when the program begins execution. Numeric variables are initialized to 0 by default
unless the programmer initialize them explicitly. Two types:
static Local variables defined in functions Keep value after function ends Only known in their own function.
extern Default for global variables and functions. used to specify that the variable is declared in a different
file. Known in any function.
The Hashemite University 7
Storage Classes -- Notes Do not use multiple storage specifiers at
the same time syntax error. E.g.: register auto double x = 0; // syntax error
mutable storage class is used with classes (in the object oriented classes).
The Hashemite University 8
Identifier Scope Rules I
Scope is where the identifier can be referenced in program.
Scope Types: File scope:
Defined outside a function, known in all functions
Examples include, global variables, function definitions and functions prototypes
Function scope: Can only be referenced inside a function
body
The Hashemite University 9
Identifier Scope Rules II Block scope:
Declared inside a block. Begins at declaration, ends at }.
Includes variables, function parameters (local variables of function).
Outer blocks “hidden” from inner blocks if same variable name is used.
Function prototype scope: Identifiers in parameter list Names in function prototype optional, and can be used
anywhere Class and namespace scopes: will be taken later.
The Hashemite University 10
1 // Fig. 3.12: fig03_12.cpp2 // A scoping example3 #include <iostream>45 using std::cout;6 using std::endl;78 void a( void ); // function prototype9 void b( void ); // function prototype10 void c( void ); // function prototype1112 int x = 1; // global variable1314 int main()15 {16 int x = 5; // local variable to main1718 cout << "local x in outer scope of main is " << x << endl;1920 { // start new scope21 int x = 7;2223 cout << "local x in inner scope of main is " << x << endl;24 } // end new scope2526 cout << "local x in outer scope of main is " << x << endl;2728 a(); // a has automatic local x29 b(); // b has static local x30 c(); // c uses global x31 a(); // a reinitializes automatic local x32 b(); // static local x retains its previous value33 c(); // global x also retains its value34
x is different inside and outside the block.
local x in outer scope of main is 5local x in inner scope of main is 7local x in outer scope of main is 5
The Hashemite University 11
3.1 Define Functions
35 cout << "local x in main is " << x << endl;3637 return 0;38 }3940 void a( void )41 {42 int x = 25; // initialized each time a is called4344 cout << endl << "local x in a is " << x 45 << " after entering a" << endl;46 ++x;47 cout << "local x in a is " << x 48 << " before exiting a" << endl;49 }5051 void b( void )52 {53 static int x = 50; // Static initialization only54 // first time b is called.55 cout << endl << "local static x is " << x 56 << " on entering b" << endl;57 ++x;58 cout << "local static x is " << x 59 << " on exiting b" << endl;60 }6162 void c( void )63 {64 cout << endl << "global x is " << x 65 << " on entering c" << endl;66 x *= 10;67 cout << "global x is " << x << " on exiting c" << endl;68 }
Local automatic variables are created and destroyed each time a is called.
Local static variables are not destroyed when the function ends.
Global variables are always accessible. Function c references the global x.
local x in a is 25 after entering a
local x in a is 26 before exiting a
local static x is 50 on entering b
local static x is 51 on exiting b
global x is 1 on entering c
global x is 10 on exiting c
The Hashemite University 12
local x in outer scope of main is 5local x in inner scope of main is 7local x in outer scope of main is 5 local x in a is 25 after entering alocal x in a is 26 before exiting a local static x is 50 on entering blocal static x is 51 on exiting b global x is 1 on entering cglobal x is 10 on exiting c local x in a is 25 after entering alocal x in a is 26 before exiting a local static x is 51 on entering blocal static x is 52 on exiting b global x is 10 on entering cglobal x is 100 on exiting clocal x in main is 5
The Hashemite University 13
Unary Scope Resolution Operator Unary scope resolution operator (::)
Access global variables if a local variable has same name
Not needed if names are different Instead of variable use ::variable Very important Cannot be used to access a
local variable of the same name in an outer block.
The Hashemite University 14
1 // Fig. 3.24: fig03_24.cpp
2 // Using the unary scope resolution operator3 #include <iostream>
4
5 using std::cout;6 using std::endl;
7
89
10
1112 const double PI = 3.14;
1314 int main()
15 {
16 const float PI = 3.14159265358979;17
18
19 cout << " Local float value of PI = " << PI20 << "\nGlobal double value of PI = " << ::PI << endl;
21
22 return 0;23 }
Local float value of PI = 3.14159265358979Global double value of PI = 3.14
Notice the use of ::
The Hashemite University 15
Additional Notes
This lecture covers the following material from the textbook: Fourth Edition
Chapter 3: Sections 3.10, 3.11, and 3.19