Computer Science 1620
description
Transcript of 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() { cout << square(3) << endl;
Function Overloading
Suppose you are in charge of writing a maximum function
The program must work with three different types: intdouble float
Function Overloading
First Way:
double maximum(double n1, double n2) {
if (n1 > n2)
return n1;
else
return n2;
}
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)
Function Overloading
Second Try:
int maximum(int n1, int n2) {
if (n1 > n2)
return n1;
else
return n2;
}
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?
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;}
Function Overloading
We can now change our original callint 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
Function Overloading
Solution: Function overloadingC++ 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
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;}
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);
Function Overloading
Resolving overloaded definitions the compiler must be able to choose
between two function declarationsalways chooses the best matchoverload resolution is a complex task
beyond scope of class
suffices to know that it tries for closest match