Chapter 8 n Scope Lifetime and More on

  • Slides: 62
Download presentation

Chapter 8. n Scope, Lifetime, and More on Functions 2/18/2021 C++程序设计 教师:鲍钰 2

Chapter 8. n Scope, Lifetime, and More on Functions 2/18/2021 C++程序设计 教师:鲍钰 2

Scope of Identifier n the scope of an identifier (or named constant) means the

Scope of Identifier n the scope of an identifier (or named constant) means the region of program code where it is legal to use that identifier for any purpose 2/18/2021 C++程序设计 教师:鲍钰 3

Local Scope vs. Global Scope the scope of an identifier that is declared inside

Local Scope vs. Global Scope the scope of an identifier that is declared inside a block (this includes function parameters) extends from the point of declaration to the end of the block 2/18/2021 the scope of an identifier that is declared outside of all namespaces, functions and classes extends from point of declaration to the end of the entire file containing program code C++程序设计 教师:鲍钰 4

const float TAX_RATE = 0. 05 ; float tip. Rate ; void handle (

const float TAX_RATE = 0. 05 ; float tip. Rate ; void handle ( int, float ) ; // global constant // global variable // function prototype using namespace std ; int main ( ) { int age ; float bill ; . . handle (age, bill) ; // age and bill local to this block // a, b, and tax cannot be used here // TAX_RATE and tip. Rate can be used return 0 ; } void handle (int a, float b) { float tax ; // a, b, and tax local to this block. // age and bill cannot be used here. // TAX_RATE and tip. Rate can be used } 2/18/2021 C++程序设计 教师:鲍钰 5

Detailed Scope Rules 1 Function name has global scope. 2 Function parameter scope is

Detailed Scope Rules 1 Function name has global scope. 2 Function parameter scope is identical to scope of a local variable declared in the outermost block of the function body. 3 Global variable (or constant) scope extends from declaration to the end of the file, except as noted in rule 5. 4 Local variable (or constant) scope extends from declaration to the end of the block where declared. This scope includes any nested blocks, except as noted in rule 5. 5 An identifier’s scope does not include any nested block that contains a locally declared identifier with the same name (local identifiers have name precedence). 2/18/2021 C++程序设计 教师:鲍钰 6

Name Precedence (or Name Hiding) n when a function declares a local identifier with

Name Precedence (or Name Hiding) n when a function declares a local identifier with the same name as a global identifier, the local identifier takes precedence within that function 2/18/2021 C++程序设计 教师:鲍钰 7

int a 1; char a 2; int main() { …} // a 1, a

int a 1; char a 2; int main() { …} // a 1, a 2 void Block 1(int a 1, char& b 2) { int c 1; int d 2; … // a 1, global a 2, b 2, c 1, d 2 } void Block 2( ) { int a 1; int b 2; … // a 1, global a 2, b 2 while(…) { int c 1; int b 2; … // c 1, b 2, local a 1, global a 2 } } 2/18/2021 C++程序设计 教师:鲍钰 8

Name Precedence Implemented by Compiler Determines Scope n n When an expression refers to

Name Precedence Implemented by Compiler Determines Scope n n When an expression refers to an identifier, the compiler first checks the local declarations. If the identifier isn’t local, compiler works outward through each level of nesting until it finds an identifier with same name. There it stops. Any identifier with the same name declared at a level further out is never reached. If compiler reaches global declarations and still can’t find the identifier, an error message results. 2/18/2021 C++程序设计 教师:鲍钰 9

Namespace Scope P 286 n the scope of an identifier declared in a namespace

Namespace Scope P 286 n the scope of an identifier declared in a namespace definition extends from the point of declaration to the end of the namespace body, and its scope includes the scope of a using directive specifying that namespace 2/18/2021 C++程序设计 教师:鲍钰 10

3 Ways to Use Namespace Identifiers n use a qualified name consisting of the

3 Ways to Use Namespace Identifiers n use a qualified name consisting of the namespace, the scope resolution operator : : and the desired the identifier alpha = std : : abs( beta ) ; n write a using declaration using std: : abs ; alpha = abs( beta ); n write a using directive locally or globally using namespace std ; alpha = abs( beta ); 2/18/2021 C++程序设计 教师:鲍钰 11

These allocate memory int some. Int ; // for the global variable int Square

These allocate memory int some. Int ; // for the global variable int Square (int n) { int result ; result = n * n ; return result ; } // for instructions in body 2/18/2021 // for the local variable C++程序设计 教师:鲍钰 12

These do NOT allocate memory int Square (int n) ; // function prototype extern

These do NOT allocate memory int Square (int n) ; // function prototype extern int some. Int ; // some. Int is global // variable defined in // another file 2/18/2021 C++程序设计 教师:鲍钰 13

Lifetime of a Variable n the lifetime of a variable is the time during

Lifetime of a Variable n the lifetime of a variable is the time during program execution when an identifier actually has memory allocated to it 2/18/2021 C++程序设计 教师:鲍钰 14

Lifetime of Local Automatic Variables n n n their storage is created (allocated) when

Lifetime of Local Automatic Variables n n n their storage is created (allocated) when control enters the function local variables are “alive” while function is executing their storage is destroyed (deallocated) when function exits 2/18/2021 C++程序设计 教师:鲍钰 15

Lifetime of Global Variables n n n their lifetime is the lifetime of the

Lifetime of Global Variables n n n their lifetime is the lifetime of the entire program their memory is allocated when program begins execution their memory is deallocated when the entire program terminates 2/18/2021 C++程序设计 教师:鲍钰 16

Automatic vs. Static Variable n storage for automatic variable is allocated at block entry

Automatic vs. Static Variable n storage for automatic variable is allocated at block entry and deallocated at block exit 2/18/2021 n storage for static variable remains allocated throughout execution of the entire program C++程序设计 教师:鲍钰 17

By default n n local variables are automatic to obtain a static local variable,

By default n n local variables are automatic to obtain a static local variable, you must use the reserved word static in its declaration. 2/18/2021 C++程序设计 教师:鲍钰 18

Static and Automatic Local Variables int popular. Square( int n) { static int times.

Static and Automatic Local Variables int popular. Square( int n) { static int times. Called = 0 ; // initialized only once int result = n * n ; // initialized each times. Called = times. Called + 1 ; cout << “Call # “ << times. Called << endl ; return result ; } 2/18/2021 C++程序设计 教师:鲍钰 19

Side effect Book P 292 The use of global variables is a poor practice.

Side effect Book P 292 The use of global variables is a poor practice. Global constants are useful. Global constant has no side effect. 2/18/2021 C++程序设计 教师:鲍钰 20

Testing and Debugging– Testing Modules Separately n n Stub – a dummy function that

Testing and Debugging– Testing Modules Separately n n Stub – a dummy function that assists in testing part of a program. Driver – a simple main( ) function that is used to call a function being tested. 2/18/2021 C++程序设计 教师:鲍钰 24

A Stub for Function Get. Year void Get. Year ( /* inout */ ifstream

A Stub for Function Get. Year void Get. Year ( /* inout */ ifstream data. In, /* out */ string& year ) // Stub to test Get. Year function in Convert. Dates program. // PRECONDITION: data. In assigned // POSTCONDITION: year assigned { cout << “Get. Year was called. Returning ” 1948”. ” << endl ; year = “ 1948” ; } 2/18/2021 C++程序设计 教师:鲍钰 25

Quick Check v p 310 2/18/2021 C++程序设计 教师:鲍钰 26

Quick Check v p 310 2/18/2021 C++程序设计 教师:鲍钰 26

Homework P 311 -313 v 6, 7, 16 v 2/18/2021 C++程序设计 教师:鲍钰 27

Homework P 311 -313 v 6, 7, 16 v 2/18/2021 C++程序设计 教师:鲍钰 27

PROGRAMMING p 313 -315 v 1, 14 v 2/18/2021 C++程序设计 教师:鲍钰 28

PROGRAMMING p 313 -315 v 1, 14 v 2/18/2021 C++程序设计 教师:鲍钰 28

TRUE/FALSE The scope of a function parameter is identical to the scope of a

TRUE/FALSE The scope of a function parameter is identical to the scope of a local variable declared in the outermost block of the function body. A) True B) False 2/18/2021 C++程序设计 教师:鲍钰 29

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 30

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 30

TRUE/FALSE The scope of an identifier does not include any nested block that contains

TRUE/FALSE The scope of an identifier does not include any nested block that contains a locally declared identifier with the same name. A) True B) False 2/18/2021 C++程序设计 教师:鲍钰 31

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 32

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 32

TRUE/FALSE If the declaration char first. Initial; appears within a block, then first. Initial

TRUE/FALSE If the declaration char first. Initial; appears within a block, then first. Initial is an automatic variable. A) True B) False 2/18/2021 C++程序设计 教师:鲍钰 33

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 34

TRUE/FALSE v TRUE 2/18/2021 C++程序设计 教师:鲍钰 34

TRUE/FALSE Using globally declared named constants is as dangerous as using globally declared variables.

TRUE/FALSE Using globally declared named constants is as dangerous as using globally declared variables. A) True B) False 2/18/2021 C++程序设计 教师:鲍钰 35

TRUE/FALSE v FALSE 2/18/2021 C++程序设计 教师:鲍钰 36

TRUE/FALSE v FALSE 2/18/2021 C++程序设计 教师:鲍钰 36

CHOICE If a variable alpha is accessible only within function F, then alpha is

CHOICE If a variable alpha is accessible only within function F, then alpha is either A) a global variable or a parameter of F. B) a local variable within F or a parameter of F. C) a global variable or an argument to F. D) a local variable within F or an argument to F. 2/18/2021 C++程序设计 教师:鲍钰 37

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 38

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 38

CHOICE What is the output of the following code fragment? (All variables are of

CHOICE What is the output of the following code fragment? (All variables are of type int. ) alpha = 3; beta = 20; if (beta > 10) { int alpha = 5; beta = beta + alpha; cout << alpha << ' ' << beta << endl; } cout << alpha << ' ' << beta << endl; A) 3 20 B) 3 25 C) 5 25 D) 5 25 3 25 2/18/2021 C++程序设计 教师:鲍钰 39

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 40

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 40

CHOICE Given the function definition void Some. Func(. . . ) { float alpha;

CHOICE Given the function definition void Some. Func(. . . ) { float alpha; . . . } which of the following statements about alpha is false? returns. A) B) C) the function. D) function. 2/18/2021 The memory allocated to alpha is deallocated when the function A parameter in the function heading can also be named alpha. The value of alpha is undefined at the moment control enters alpha cannot be accessed directly from code outside the C++程序设计 教师:鲍钰 41

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 42

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 42

CHOICE Which of the following statements about global variables is true? A) A global

CHOICE Which of the following statements about global variables is true? A) A global variable is accessible only to the main function. B) A global variable is declared in the highest-level block in which it is used. C) A global variable can have the same name as a variable that is declared locally within a function. D) If a function contains a local variable with the same name as a global variable, the global variable takes precedence. 2/18/2021 C++程序设计 教师:鲍钰 43

CHOICE v C 2/18/2021 C++程序设计 教师:鲍钰 44

CHOICE v C 2/18/2021 C++程序设计 教师:鲍钰 44

CHOICE In the following function, the declaration of beta includes an initialization. void Some.

CHOICE In the following function, the declaration of beta includes an initialization. void Some. Func( int alpha ) { int beta = 25; . . . } Which of the following statements about beta is false? A) It is initialized once only, the first time the function is called. B) It is initialized each time the function is called. C) It cannot be reassigned a different value within the function. D) a and c above E) b and c above 2/18/2021 C++程序设计 教师:鲍钰 45

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 46

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 46

CHOICE In the following function, the declaration of beta includes an initialization. void Some.

CHOICE In the following function, the declaration of beta includes an initialization. void Some. Func( int alpha ) { static int beta = 25; . . . } Which of the following statements about beta is false? A) It is initialized once only, the first time the function is called. B) It is initialized each time the function is called. C) It cannot be reassigned a different value within the function. D) a and c above E) b and c above 2/18/2021 C++程序设计 教师:鲍钰 47

CHOICE v E 2/18/2021 C++程序设计 教师:鲍钰 48

CHOICE v E 2/18/2021 C++程序设计 教师:鲍钰 48

CHOICE Given the function definition void Test( /* in */ int alpha ) {

CHOICE Given the function definition void Test( /* in */ int alpha ) { static int n = 5; n = n + alpha; cout << n << ' '; } what is the output of the following code? (Assume that Test has not been called previously. ) Test(20); Test(30); A) 20 30 B) 25 35 C) 20 50 D) 25 55 E) 25 60 2/18/2021 C++程序设计 教师:鲍钰 49

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 50

CHOICE v D 2/18/2021 C++程序设计 教师:鲍钰 50

CHOICE What happens if a value-returning function with the prototype float Average( int, int);

CHOICE What happens if a value-returning function with the prototype float Average( int, int); is called by using the following statement? (alpha and beta are int variables. ) Average(alpha, 34, beta); The compiler issues a syntax error message. The function is executed, and the function value is A) B) discarded. C) The function is executed, and the function value is assigned to alpha. D) The function is not executed, and the program halts with a run-time error message. 2/18/2021 C++程序设计 教师:鲍钰 51

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 52

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 52

CHOICE For the function definition int Some. Func( /* in */ int alpha, /*

CHOICE For the function definition int Some. Func( /* in */ int alpha, /* in */ int beta ) { int gamma; alpha = alpha + beta; gamma = 2 * alpha; return gamma; } what is the function postcondition? A) // Postcondition: gamma == 2*alpha B) // Postcondition: alpha == alpha@entry + beta // && gamma == 2*alpha C) // Postcondition: Function value == gamma D) // Postcondition: Function value == 2*alpha E) // Postcondition: Function value == 2*(alpha@entry + beta) 2/18/2021 C++程序设计 教师:鲍钰 53

CHOICE v E 2/18/2021 C++程序设计 教师:鲍钰 54

CHOICE v E 2/18/2021 C++程序设计 教师:鲍钰 54

CHOICE If a module is supposed to compute the average of five numbers, which

CHOICE If a module is supposed to compute the average of five numbers, which is more appropriate to use--a value-returning function or a void function? A) a value-returning function B) a void function 2/18/2021 C++程序设计 教师:鲍钰 55

CHOICE v A 2/18/2021 C++程序设计 教师:鲍钰 56

CHOICE v A 2/18/2021 C++程序设计 教师:鲍钰 56

CHOICE If a module is supposed to convert five values measured in inches to

CHOICE If a module is supposed to convert five values measured in inches to their equivalent measures in centimeters, which is more appropriate to use--a value-returning function or a void function? A) a value-returning function B) a void function 2/18/2021 C++程序设计 教师:鲍钰 57

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 58

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 58

CHOICE If a module is supposed to print a line of asterisks of a

CHOICE If a module is supposed to print a line of asterisks of a given length, which is more appropriate to use--a value-returning function or a void function? A) a value-returning function B) a void function 2/18/2021 C++程序设计 教师:鲍钰 59

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 60

CHOICE v B 2/18/2021 C++程序设计 教师:鲍钰 60

CHOICE The function float Distance( /* in */ float velocity, /* in */ float

CHOICE The function float Distance( /* in */ float velocity, /* in */ float angle, /* in */ float resistance ) { cout << "Distance was called. Returning 48. 5" << endl; return 48. 5; } is an example of: A) a driver B) a plug C) a stub D) a void function 2/18/2021 C++程序设计 教师:鲍钰 61

CHOICE v C 2/18/2021 C++程序设计 教师:鲍钰 62

CHOICE v C 2/18/2021 C++程序设计 教师:鲍钰 62