ImageVerifierCode 换一换
格式:PPT , 页数:16 ,大小:76.50KB ,
资源ID:378084      下载积分:2000 积分
快捷下载
登录下载
邮箱/手机:
温馨提示:
如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝扫码支付 微信扫码支付   
注意:如需开发票,请勿充值!
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【http://www.mydoc123.com/d-378084.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Advanced Topics in Functions.ppt)为本站会员(李朗)主动上传,麦多课文库仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文库(发送邮件至master@mydoc123.com或直接QQ联系客服),我们立即给予删除!

Advanced Topics in Functions.ppt

1、1,Advanced Topics in Functions,Lecture 14,2,3.19 Unitary Scope Resolution Operator,Unary scope resolution operator (:) Access global variable if local variable has same name Not needed if names are different Use :variable y = :x + 3; Good to avoid using same names for locals and globals,3,fig03_24.c

2、pp (1 of 2),1 / Fig. 3.24: fig03_24.cpp 2 / Using the unary scope resolution operator. 3 #include 4 5 using std:cout; 6 using std:endl; 7 8 #include 9 10 using std:setprecision; 11 12 / define global constant PI 13 const double PI = 3.14159265358979; 14 15 int main() 16 17 / define local constant PI

3、 18 const float PI = static_cast( :PI ); 19 20 / display values of local and global PI constants 21 cout setprecision( 20 ) 22 “ Local float value of PI = “ PI 23 “nGlobal double value of PI = “ :PI endl; 24 25 return 0; / indicates successful termination,4,fig03_24.cpp (2 of 2) fig03_24.cpp output

4、(1 of 1),26 27 / end main,Borland C+ command-line compiler output:Local float value of PI = 3.141592741012573242 Global double value of PI = 3.141592653589790007Microsoft Visual C+ compiler output:Local float value of PI = 3.1415927410125732 Global double value of PI = 3.14159265358979,5,3.20 Functi

5、on Overloading,Function overloading Functions with same name and different parameters Should perform similar tasks I.e., function to square ints and function to square floatsint square( int x) return x * x;float square(float x) return x * x; Overloaded functions distinguished by signature Based on n

6、ame and parameter types (order matters) Name mangling Encodes function identifier with parameters Type-safe linkage Ensures proper overloaded function called,6,fig03_25.cpp (1 of 2),1 / Fig. 3.25: fig03_25.cpp 2 / Using overloaded functions. 3 #include 4 5 using std:cout; 6 using std:endl; 7 8 / fun

7、ction square for int values 9 int square( int x ) 10 11 cout “Called square with int argument: “ x endl; 12 return x * x; 13 14 / end int version of function square 15 16 / function square for double values 17 double square( double y ) 18 19 cout “Called square with double argument: “ y endl; 20 ret

8、urn y * y; 21 22 / end double version of function square 23,7,fig03_25.cpp (2 of 2) fig03_25.cpp output (1 of 1),24 int main() 25 26 int intResult = square( 7 ); / calls int version 27 double doubleResult = square( 7.5 ); / calls double version 28 29 cout “nThe square of integer 7 is “ intResult 30

9、“nThe square of double 7.5 is “ doubleResult 31 endl; 32 33 return 0; / indicates successful termination 34 35 / end main,Called square with int argument: 7 Called square with double argument: 7.5The square of integer 7 is 49 The square of double 7.5 is 56.25,8,fig03_26.cpp (1 of 2),1 / Fig. 3.26: f

10、ig03_26.cpp 2 / Name mangling. 3 4 / function square for int values 5 int square( int x ) 6 7 return x * x; 8 9 10 / function square for double values 11 double square( double y ) 12 13 return y * y; 14 15 16 / function that receives arguments of types 17 / int, float, char and int * 18 void nothing

11、1( int a, float b, char c, int *d ) 19 20 / empty function body 21 22,9,fig03_26.cpp (2 of 2) fig03_26.cpp output (1 of 1),23 / function that receives arguments of types 24 / char, int, float * and double * 25 char *nothing2( char a, int b, float *c, double *d ) 26 27 return 0; 28 29 30 int main() 3

12、1 32 return 0; / indicates successful termination 33 34 / end main,_main nothing2$qcipfpd nothing1$qifcpi square$qd square$qi,10,3.21 Function Templates,Compact way to make overloaded functions Generate separate function for different data types Format Begin with keyword template Formal type paramet

13、ers in brackets Every type parameter preceded by typename or class (synonyms) Placeholders for built-in types (i.e., int) or user-defined types Specify arguments types, return types, declare variables Function definition like normal, except formal types used,11,3.21 Function Templates,Example templa

14、te / or template T square( T value1 ) return value1 * value1; T is a formal type, used as parameter type Above function returns variable of same type as parameter In function call, T replaced by real type If int, all Ts become ints int x; int y = square(x);,12,fig03_27.cpp (1 of 3),1 / Fig. 3.27: fi

15、g03_27.cpp 2 / Using a function template. 3 #include 4 5 using std:cout; 6 using std:cin; 7 using std:endl; 8 9 / definition of function template maximum 10 template / or template 11 T maximum( T value1, T value2, T value3 ) 12 13 T max = value1; 14 15 if ( value2 max ) 16 max = value2; 17 18 if ( v

16、alue3 max ) 19 max = value3; 20 21 return max; 22 23 / end function template maximum 24,13,fig03_27.cpp (2 of 3),25 int main() 26 27 / demonstrate maximum with int values 28 int int1, int2, int3; 29 30 cout int1 int2 int3; 32 33 / invoke int version of maximum 34 cout double1 double2 double3; 42 43

17、/ invoke double version of maximum 44 cout “The maximum double value is: “ 45 maximum( double1, double2, double3 ); 46,14,47 / demonstrate maximum with char values 48 char char1, char2, char3; 49 50 cout char1 char2 char3; 52 53 / invoke char version of maximum 54 cout “The maximum character value i

18、s: “ 55 maximum( char1, char2, char3 ) 56 endl; 57 58 return 0; / indicates successful termination 59 60 / end main,fig03_27.cpp (3 of 3),15,Input three integer values: 1 2 3 The maximum integer value is: 3Input three double values: 3.3 2.2 1.1 The maximum double value is: 3.3Input three characters:

19、 A C B The maximum character value is: C,fig03_27.cpp output (1 of 1),16,Final Review,Comprehensive Exam Youll have 2 hours to work on it Topics Classes Writing to/Reading from Files Pointers Control Structures Loops Conditionals,More Topics Arithmetic, casting, and type precedence Arrays 1 and 2D string class Recursion,

copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
备案/许可证编号:苏ICP备17064731号-1