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,