1 / 14

Computer Science 1620

Computer Science 1620. Function Overloading. 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() {

keola
Download Presentation

Computer Science 1620

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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?

  8. 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; }

  9. 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

  10. 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

  11. 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; }

  12. 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);

  13. 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

More Related