《C++程序设计》(英文版) Chapter 8 Topics

Programming in c++ Scope, Lifetime, and More on Functions Dale/eems/Headington
1 Scope, Lifetime, and More on Functions

Programming in C++ Chapter 8 Topics s Local Scope vS. Global Scope of an Identifier Detailed Scope Rules to Determine which Variables are Accessible in a block Determining the Lifetime of a Variable Initializations in Declarations s Interface Design %Writing a Value-Returning Function for a Task Some Value-Returning Functions with Prototypes in Header Files ectype and cmath Creating and Using a Module Structure Chart Stub Testing a Program
2 Chapter 8 Topics ❖Local Scope vs. Global Scope of an Identifier ❖Detailed Scope Rules to Determine which Variables are Accessible in a Block ❖Determining the Lifetime of a Variable ❖Initializations in Declarations ❖Interface Design ❖Writing a Value-Returning Function for a Task ❖Some Value-Returning Functions with Prototypes in Header Files cctype and cmath ❖Creating and Using a Module Structure Chart ❖Stub Testing a Program

Programming in C++ Scope of Identifier o 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
3 Scope of Identifier ❖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

Programming in C++ Local Scope VS. Global cope o the scope of an ☆ the scope of ar identifier that is identifier that is declared inside a declared outside of block(this includes all namespaces, function parameters) functions and classes extends from the extends from point of point of declaration declaration to the end to the end of the of the entire file block containing program code
4 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 ❖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

Programming in C++ const float TAX RATE =0.05; / global constant float tipRate ∥ global variable void handle( int, float ) ∥ function prototype using namespace std int main( int age //age and bill local to this block float bill i ma b, and tax cannot be used here //TAX RATE and tip Rate can be used handle(age, bill); return 0; void handle(int a, float b) float tax m/a b and tax local to this block l/ age and bill cannot be used here //TAX RATE and tip Rate can be used 5
5 const float TAX_RATE = 0.05 ; // global constant float tipRate ; // global variable void handle ( int, float ) ; // function prototype using namespace std ; int main ( ) { int age ; // age and bill local to this block float bill ; . // a, b, and tax cannot be used here . // TAX_RATE and tipRate can be used handle (age, bill) ; 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 tipRate can be used }

Programming in C++ Name Precedence (or Name Hiding) %when a function declares a local identifier with the same name as a global identifier, the local identifier takes precedence within that function
6 Name Precedence (or Name Hiding) ❖when a function declares a local identifier with the same name as a global identifier, the local identifier takes precedence within that function

Programming in C++ 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)
7 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)

Programming in C++ Figure Scope Diagram for Scope Rules Program int a1 char a2. int main() void block1( int a1 char& b2) int c1 int d2, void block2() int a1 int b2. while(-) ∥/ Block3 C1 int b2
8 Figure Scope Diagram for Scope Rules Program int a1; char a2; int main ( ) { } void block1 ( { } } void block2 ( ) { int a1, char& b2 ) int c1; int d2; int a1; int b2; while (…) { //Block3 } int c1; int b2;

Programming in C++ Name Precedence Implemented by Compiler Determines Scope . When an expression refers to an identifier, the compiler first checks the local declarations s 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 o Any identifier with the same name declared at a level further out is never reached o If compiler reaches global declarations and still can't find the identifier, an error message results
9 Name Precedence Implemented by Compiler Determines Scope ❖ 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

Programming in C++ Namespace Scope o 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 10
10 Namespace Scope ❖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
按次数下载不扣除下载券;
注册用户24小时内重复下载只扣除一次;
顺序:VIP每日次数-->可用次数-->下载券;
- 《C++程序设计》(英文版) Chapter 7 Topics.ppt
- 《C++程序设计》(英文版) Chapter 6 Topics.ppt
- 《C++程序设计》(英文版) Chapter 5 Topics.ppt
- 《C++程序设计》(英文版) Chapter 4 Topics.ppt
- 《C++程序设计》(英文版) Chapter 3 Topics.ppt
- 《C++程序设计》(英文版) Chapter 2 Topics.ppt
- 《C++程序设计》(英文版) Chapter 1 Topics.ppt
- 《网络互连技术教程》第9章 广播.ppt
- 《网络互连技术教程》第8章 用户数据报协议—UDP.ppt
- 《网络互连技术教程》第7章 CMP和网络状态.ppt
- 《网络互连技术教程》第6章 地址解析.ppt
- 《网络互连技术教程》第5章 子网与超网.ppt
- 《网络互连技术教程》第4章 网络互连协议——IP.ppt
- 《网络互连技术教程》第3章 网络接入技术.ppt
- 《网络互连技术教程》第2章 网络互连体系.ppt
- 《网络互连技术教程》第1章 网络的历史与发展.ppt
- 《网络互连技术教程》第12章 应用服务.ppt
- 《网络互连技术教程》第11章 域名服务系统—DNS.ppt
- 《网络互连技术教程》第10章 传输控制协议—7CP.ppt
- 高职高专课程:《数据库应用技术(SQL SERVER2000)》电子教案(PPT教学课件)第9章 存储过程与触发器.ppt
- 《C++程序设计》(英文版) Chapter 9 Topics.ppt
- 《C++程序设计》(英文版) Chapter 10 Topics.ppt
- 《C++程序设计》(英文版) Chapter 11 Topics.ppt
- 《C++程序设计》(英文版) Chapter 12 Topic.ppt
- 《C++程序设计》(英文版) Chapter 13 Topics.ppt
- 《C++程序设计》(英文版) Chapter 14 Topics.ppt
- 《C++程序设计》(英文版) Chapter 15 Topics.ppt
- 《Java编程技术基础》第一章 面向对象原理与实现.ppt
- 《Java编程技术基础》第二章 Java的实现基础.ppt
- 《Java编程技术基础》第二章习题.doc
- 《Java编程技术基础》第三章 类与对象(一).ppt
- 《Java编程技术基础》第三章习题.doc
- 《Java编程技术基础》第四章 类与对象(二).ppt
- 《Java编程技术基础》第四章习题.doc
- 《Java编程技术基础》第五章 异常与垃圾收集.ppt
- 《Java编程技术基础》第五章习题.doc
- 《Java编程技术基础》第十一章 Java集合框架.ppt
- 《Java编程技术基础》第十一章习题.doc
- 《Java编程技术基础》第二章 JDBC.ppt
- 武汉外语外事职业学院:《VisualBasic语言程序设计教程》第二版 第14章 图形处理.ppt