L05_OLT - Overloading and Templates Nyhoff: 9 Deitel:...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Overloading and Templates Nyhoff: 9 Deitel: 6,11,14 COMP152 Spring 2010 D.Y. Yeung Outline COMP152 2  Function overloading  Operator overloading and copy constructor  An example on string  Function templates  Class templates Function Overloading  Overloaded functions have  Same name  Different sets of parameters  Compiler selects proper function to execute based on number, types and order of arguments in the function call  Commonly used to create several functions of the same name that perform similar tasks, but on different data types and numbers of parameters COMP152 3 overload.cpp (1/2)  Defining a square function for ints and doubles COMP152 4 // function square for int values int square( int x ) { cout << "square of integer " << x << " is "; return x * x; } // end function square with int argument // function square for double values double square( double y ) { cout << "square of double " << y << " is "; return y * y; } // end function square with double argument overload.cpp (2/2)  Sample Output  Output confirms that the proper function was called in each case COMP152 5 int main() { cout << square( 7 ); // calls int version cout << endl; cout << square( 7.5 ); // calls double version cout << endl; return 0; // indicates successful termination } // end main square of integer 7 is 49 overload.cpp (2/2)  Sample Output  Output confirms that the proper function was called in each case COMP152 5 int main() { cout << square( 7 ); // calls int version cout << endl; cout << square( 7.5 ); // calls double version cout << endl; return 0; // indicates successful termination } // end main square of integer 7 is 49 square of double 7.5 is 56.25 More on Function Overloading  C++ allows function overloading #include <iostream> #include <cstdlib> using namespace std; int max(int a, int b) { return (a > b ? a : b); } char* max(char* a, char* b) { return (strcmp(a, b) > 0 ? a : b); } int main() { cout << "max(19, 69) = " << max(19, 69) << endl; cout << "max(\"abc\", \"def\") = " << max("abc", "def") << endl; return 0; } Function Overloading  How the compiler differentiates overloaded functions:  Overloaded functions are distinguished by their signatures  Compiler encodes each function identifier with the number and types of its parameters to enable type-safe linkage  Type-safe linkage ensures that  Proper overloaded function is called  Types of the arguments conform to types of the parameters  Creating overloaded functions with identical parameter lists but different return types is a compilation error  It is ambiguous on which function to call COMP152 7 Operator Overloading  Use operators with objects (operator overloading)  Clearer than function calls for certain classes  Operator sensitive to context/class objects  Examples  <<  Stream insertion  bitwise left-shift ( i << 2 )  +, -, *, /  Performs arithmetic on multiple items (integers, floats, etc.)  Global functions:...
View Full Document

This note was uploaded on 08/25/2010 for the course COMP COMP152 taught by Professor D.y.yeung during the Spring '10 term at HKUST.

Page1 / 80

L05_OLT - Overloading and Templates Nyhoff: 9 Deitel:...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online