C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions

上传人:努力****83 文档编号:187469584 上传时间:2023-02-14 格式:PPT 页数:61 大小:374.50KB
收藏 版权申诉 举报 下载
C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions_第1页
第1页 / 共61页
C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions_第2页
第2页 / 共61页
C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions_第3页
第3页 / 共61页
资源描述:

《C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions》由会员分享,可在线阅读,更多相关《C++程序设计课件:Chapter8 Scope, Lifetime, and More on Functions(61页珍藏版)》请在装配图网上搜索。

1、2/14/2023C+程序设计 教师:1Chapter 8.nScope,Lifetime,and More on Functions2/14/2023C+程序设计 教师:2Scope of Identifiernthe 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/14/2023C+程序设计 教师:3Local Scope vs.Global Scopethe scope o

2、f 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

3、containing program code 2/14/2023C+程序设计 教师:4const float TAX_RATE=0.05;/global constantfloat tipRate;/global variablevoid handle(int,float);/function prototypeusing 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 ca

4、n 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 used2/14/2023C+程序设计 教师:5Detailed Scope Rules1Function name has global scope.2 Function parameter scope is identical to scope of a

5、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

6、 nested blocks,except as noted in rule 5.5 An identifiers scope does not include any nested block that contains a locally declared identifier with the same name(local identifiers have name precedence).2/14/2023C+程序设计 教师:6Name Precedence(or Name Hiding)nwhen a function declares a local identifier wit

7、h the same name as a global identifier,the local identifier takes precedence within that function 2/14/2023C+程序设计 教师:7 int a1;char a2;int main()/a1,a2 void Block1(int a1,char&b2)int c1;int d2;/a1,global a2,b2,c1,d2 void Block2()int a1;int b2;/a1,global a2,b2 while()int c1;int b2;/c1,b2,local a1,glob

8、al a2 2/14/2023C+程序设计 教师:8Name Precedence Implemented by Compiler Determines ScopenWhen an expression refers to an identifier,the compiler first checks the local declarations.nIf the identifier isnt local,compiler works outward through each level of nesting until it finds an identifier with same nam

9、e.There it stops.nAny identifier with the same name declared at a level further out is never reached.nIf compiler reaches global declarations and still cant find the identifier,an error message results.2/14/2023C+程序设计 教师:9Namespace Scope P286nthe scope of an identifier declared in a namespace defini

10、tion 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/14/2023C+程序设计 教师:103 Ways to Use Namespace Identifiersnuse a qualified name consisting of the namespace,the scope resolution operator:and the

11、desired the identifier alpha =std:abs(beta);nwrite a using declaration using std:abs;alpha =abs(beta);nwrite a using directive locally or globally using namespace std;alpha =abs(beta);2/14/2023C+程序设计 教师:11These allocate memoryint someInt;/for the global variableint Square(int n)/for instructions in

12、body int result;/for the local variable result =n*n;return result;2/14/2023C+程序设计 教师:12These do NOT allocate memoryint Square(int n);/function prototypeextern int someInt;/someInt is global/variable defined in /another file2/14/2023C+程序设计 教师:13Lifetime of a Variablenthe lifetime of a variable is the

13、 time during program execution when an identifier actually has memory allocated to it 2/14/2023C+程序设计 教师:14Lifetime of Local Automatic Variablesntheir storage is created(allocated)when control enters the functionnlocal variables are“alive”while function is executingntheir storage is destroyed(deallo

14、cated)when function exits2/14/2023C+程序设计 教师:15Lifetime of Global Variablesntheir lifetime is the lifetime of the entire programntheir memory is allocated when program begins executionntheir memory is deallocated when the entire program terminates2/14/2023C+程序设计 教师:16Automatic vs.Static Variablenstor

15、age for automatic variable is allocated at block entry and deallocated at block exitnstorage for static variable remains allocated throughout execution of the entire program2/14/2023C+程序设计 教师:17By default nlocal variables are automatic nto obtain a static local variable,you must use the reserved wor

16、d static in its declaration.2/14/2023C+程序设计 教师:18Static and Automatic Local Variablesint popularSquare(int n)static int timesCalled=0;/initialized only once int result =n*n;/initialized each time timesCalled =timesCalled+1;cout “Call#“timesCalled endl;return result;2/14/2023C+程序设计 教师:19Side effectBo

17、ok P2922/14/2023C+程序设计 教师:20Some Prototypes in Header File int isalpha(char ch);/FCTNVAL =nonzero,if ch is an alphabet letter /=zero,otherwiseint isdigit(char ch);/FCTNVAL=nonzero,if ch is a digit(0-9)/=zero,otherwiseint islower(char ch);/FCTNVAL=nonzero,if ch is a lowercase letter(a-z)/=zero,otherw

18、iseint isupper(char ch);/FCTNVAL=nonzero,if ch is an uppercase letter(A-Z)/=zero,otherwisep2972/14/2023C+程序设计 教师:21When to Use Value-Returning Functions1 If it must return more than one value or modify any of the callers arguments,do not use a value-returning function.2 If it must perform I/O,do not

19、 use a value-returning function.3 If there is only one value returned,and it is Boolean,a value-returning function is appropriate.4 If there is only one value returned,and that value will be used immediately in an expression,a value-returning function is appropriate.5 When in doubt,use a void functi

20、on.You can recode any value-returning function as a void function by adding an extra outgoing parameter.6 If both void and value-returning are acceptable,use the one you prefer.2/14/2023C+程序设计 教师:22/*in*/value parameter/*out*/reference parameterusing&/*inout*/reference parameterusing&What will the f

21、unction do with your argument?will only use its valuewill give it a valuewill change its valueIF THE FUNCTION-FUNCTION PARAMETER IS-NOTE:I/O stream variables and arrays are exceptions2/14/2023C+程序设计 教师:232/14/2023C+程序设计 教师:24A Stub for Function GetYearvoid GetYear(/*inout*/ifstream dataIn,/*out*/str

22、ing&year)/Stub to test GetYear function in ConvertDates program./PRECONDITION:dataIn assigned/POSTCONDITION:year assignedcout “GetYear was called.Returning”1948”.”10)int alpha=5;beta=beta+alpha;cout alpha beta endl;cout alpha beta endl;A)3 20 B)3 253 25 C)5 255 25 D)5 253 25 2/14/2023C+程序设计 教师:37CHO

23、ICEvD2/14/2023C+程序设计 教师:38CHOICE Given the function definition void SomeFunc(.)float alpha;.which of the following statements about alpha is false?A)The memory allocated to alpha is deallocated when the function returns.B)A parameter in the function heading can also be named alpha.C)The value of alp

24、ha is undefined at the moment control enters the function.D)alpha cannot be accessed directly from code outside the function.2/14/2023C+程序设计 教师:39CHOICEv B2/14/2023C+程序设计 教师:40CHOICE Which of the following statements about global variables is true?A)A global variable is accessible only to the main f

25、unction.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 prece

26、dence.2/14/2023C+程序设计 教师:41CHOICEv C2/14/2023C+程序设计 教师:42CHOICE In the following function,the declaration of beta includes an initialization.void SomeFunc(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.

27、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/14/2023C+程序设计 教师:43CHOICEv D2/14/2023C+程序设计 教师:44CHOICE In the following function,the declaration of beta includes an initialization.void SomeFunc(in

28、t 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

29、/14/2023C+程序设计 教师:45CHOICEvE2/14/2023C+程序设计 教师:46CHOICE 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/14/2

30、023C+程序设计 教师:47CHOICEv D2/14/2023C+程序设计 教师:48CHOICE What happens if a value-returning function with the prototype float Average(int,int,int);is called by using the following statement?(alpha and beta are int variables.)Average(alpha,34,beta);A)The compiler issues a syntax error message.B)The functio

31、n is executed,and the function value is 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/14/2023C+程序设计 教师:49CHOICEv B2/14/2023C+程序设计 教师:50CHOICE For the function definition int SomeFu

32、nc(/*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=alphaentry+beta /&gamma=2*alpha C)/Postcondition:Function value=gamma D)/Postcondition:Function value=2*alpha E)/Postcondi

33、tion:Function value=2*(alphaentry+beta)2/14/2023C+程序设计 教师:51CHOICEv E2/14/2023C+程序设计 教师:52CHOICE 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/14/2023C+程序设

34、计 教师:53CHOICEv A2/14/2023C+程序设计 教师:54CHOICE 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/14/2023C+程序设计 教师:5

35、5CHOICEvB2/14/2023C+程序设计 教师:56CHOICE 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/14/2023C+程序设计 教师:57CHOICEv B2/14/2023C+程序设计 教师:58CHOICE The func

36、tion 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/14/2023C+程序设计 教师:59CHOICEv C2/14/2023C+程序设计 教师:60上机课作业预习nChapter 8n25.Book p313 1,11,14n26.Programming Problems Book P315 32/14/2023C+程序设计 教师:61课后在线平台评测WWW.ECNUCPP.COM

展开阅读全文
温馨提示:
1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
2: 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
3.本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!