CS 107 - Lab 6

Overloading Functions

In C++, you can write multiple functions with the same name provided the parameters are different. A good example of overloading is the division operator. If both operands are integers, integer division occurs. If at least one of the parameters is a floating point number, floating point division occurs.

In functions this can be nice. Let us say we want a function called max() which will return the larger of two numbers. Since parameters for functions must have a particular data type assigned with them (int, float, double, etc.), a function to find the larger integer value could not be used to find the larger float value. If we could not overload functions, we would have to name one something like "maxInt()" and other "maxFloat()". With overloading, we would still need to write multiple functions, but they can all have the same name. Thus when we want to use this function, we just have to remember it is called "max()" no matter what data type is used.

We can also write a function max() to return the larger of two numbers and another function max() to return the largest of three numbers and another function max() to return the largest of four numbers....

Lab Assignment

Due: By your Lab Time during the seventh week of the semester (the week of 3/3/2003).

You may turn in the assignment to your TA during lab or place it in his mailbox in 905 SEO. It is suggested that you place it in his mailbox just in case you are unable to attend lab. You are to hand in a print out of your program to do the following to your TA.

Create a C++ program that will overload the function min() to allow it to return the smallest of two, three or four numbers. These numbers can be either integers or floating points. Write a driver program that will test out your functions.

Hand in a print out of your program to your TA.