L10 - function overloading and function templates

L10 - function overloading and function templates -...

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

View Full Document Right Arrow Icon
Function Overloading • C++ supports writing more than one function with the same name but different argument lists – you’ve seen this a little bit before with overloaded operators and functions last semester int a = 3, b = 2; double c = 3; cout << a/b << endl << c / b; here the same function / returns different values when applied to the same values (though different types) – overloaded functions have the same name and perform different (but usually similar) tasks • How does the compiler know which one the programmer is calling? – they have different signatures
Background image of page 1

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

View Full DocumentRight Arrow Icon
Function Signatures and Overloading – A function’s signature is a combination of its name and its argument list int area (int height, int width) { return height * width;} double area (double height, double width) {return height * width;} double area (double radius) { return 3.14159 * radius;} int area (double height, double width) {return static_cast <int> (height * width);} – the return type is not part of the signature – so you can’t overload just by changing return type signature: @area $q d d signature: @area $q d signature: @area $q i i signature: @area $q d d
Background image of page 2
When you use function overloading, ambiguity can happen in 3 cases: 1. if you have call by reference and call by value with the same parameters’ types: { x = x * x;} void square (double x) { cout << x * x; } if the call is: double a = 4.5; square (a); what happens? does this mean square(a) or square (&a)? The compiler will generate an ambiguous call error but if you do something like square(4.5), no such error will be generated
Background image of page 3

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

View Full DocumentRight Arrow Icon
2. if there is no matching parameter type for a called function but variables can be automatically converted to appropriate types: int area (int height, int width) { return height * width; } double area (double height, double width) { return height * width; } here, if the call in, say, main is: area (2.5, 3); there is an ambiguous situation and the Visual C++ compiler will generate an error – it will tell you that two overloads have similar conversions – this is a fatal error
Background image of page 4
3. if you use default arguments:
Background image of page 5

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

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

Page1 / 19

L10 - function overloading and function templates -...

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

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