Download presentation

Presentation is loading. Please wait.

Published byEvan Piersall Modified about 1 year ago

1
Computer Science 1620 Function Overloading

2
Review Question: suppose I have the following function: can I call this function with an integer? yes – compiler will promote integer to double same thing applies to demotion double square(double x) { return x * x; } int main() { cout << square(3) << endl;

3
Function Overloading Suppose you are in charge of writing a maximum function The program must work with three different types: int double float

4
Function Overloading First Way: double maximum(double n1, double n2) { if (n1 > n2) return n1; else return n2; }

5
Function Overloading The previous example works fine, but inefficient consider following call int x, y; cin >> x >> y; int z = maximum(x,y); consider what happens when compiling the program the values x and y must be converted to type double in order to use the function the return value is of type double … which must be converted back to an integer (which generates a compiler warning)

6
Function Overloading Second Try: int maximum(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

7
Function Overloading The previous example works fine for integers, but what about doubles? consider following call double x, y; cin >> x >> y; double z = maximum(x,y); cout << z << endl; suppose I run this program, and type in the values 4.6 and 7.4 what will the output be?

9
Function Overloading Second try: double maximumDouble(double n1, double n2) { if (n1 > n2) return n1; else return n2; } float maximumFloat(float n1, float n2) { if (n1 > n2) return n1; else return n2; } int maximumInt(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

10
Function Overloading We can now change our original call int x, y; cin >> x >> y; int z = maximumInt(x,y); no conversions take place some problems though what if a programmer writes a program using integers … and the supervisor decides to use doubles instead not only are you changing your variable types but now you have to change all of your calls to maximum

11
Function Overloading Solution: Function overloading C++ allows you to use the same function name for different functions the functions must have different parameter lists the function being called will depend on the arguments being sent

12
Function Overloading Last try: double maximum(double n1, double n2) { if (n1 > n2) return n1; else return n2; } float maximum(float n1, float n2) { if (n1 > n2) return n1; else return n2; } int maximum(int n1, int n2) { if (n1 > n2) return n1; else return n2; }

13
Function Overloading Now when we make the following call: int x, y; cin >> x >> y; int z = maximum(x,y); no conversions take place if we wanted to change to doubles, we need only change the variable type double x, y; cin >> x >> y; double z = maximum(x,y);

14
Function Overloading Resolving overloaded definitions the compiler must be able to choose between two function declarations always chooses the best match overload resolution is a complex task beyond scope of class suffices to know that it tries for closest match

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google