Advanced Topics in Functions.ppt
《Advanced Topics in Functions.ppt》由会员分享,可在线阅读,更多相关《Advanced Topics in Functions.ppt(16页珍藏版)》请在麦多课文档分享上搜索。
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
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- ADVANCEDTOPICSINFUNCTIONSPPT
