Program 4

Home Windows C++ 241 C++ 242 Contact Info Contents

Program 4 - January 29

Up

Note: This assignment was originally written in a way that was not possible to carry out in Visual C++ 6.0. It had worked well in some earlier versions, but some changes in 6.0 to put it in conformity with the C++ standard made what had previously worked ambiguous as far as the compiler was concerned. You should do this version if you are working in Visual C++. If you are working in a different compiler, I don't know whether the original will work, but it would be an interesting experiment to try it out.

If you want to try the original, or just need to compare it with this one, you can see it here. The changes from the original are also highlighted on this page by use of bold italics (like this).

Function Overloading

This assignment allows you to test the use of overloading to define a function in several versions. The functions you will be writing are very simple ones but they allow you to prove that overloading works.

Write a function to add two numbers and return the result. You will write four versions of this function: one for each combination of int and double.

Now write functions that do the same thing for multiplication. The four function prototypes you will end up with are:

int add(int, int);
double add(double, double);
double add(int, double);
double add(double, int);

int times(int, int);
double times(double, double);
double times(int, double);
double times(double, int);

In order to see what is happening, it will be necessary for your functions to print out some information. Put a line in each of the functions similar to the following:

cout << "add(int,int) called with arguments" 
       << arg1 << " and " << arg2 << endl;

Here is an outline of your main program. You'll have to add some things to make it work and you can use your own numbers.

int main()
{
    cout << "Calculating 3 + 5 * 7.0\n"
    cout << "The answer should be " << 3 + 5 * 7.0 << "\n";
    double result = Add(3, Times(5 * 7.0));
    cout << "The answer is " result;

    // Repeat for other expressions. Do at least five of them.
    // Make sure one is all ints  and another is all doubles but
    // mix the types in the rest. 
   
// Make sure that all eight of your functions get called.

    return 0
}

What to Turn In

For this assignment, you will turn in some extra material. You should send me the following:

Your source file (you can do this in one file).

Your output (the easiest way to do this is after you get your program working is to redirect the output to a file from the command line. Ask in class if you don't know how. 

A note or document (text or word are Ok) that answers the following questions:

  1. In what order did the compiler call your functions. That is, can you form a general rule that tells how functions are called when you use one function call as an argument to another function call.

  2. You wrote four of each function. How did the compiler decide which to call?

  3. If you did the original assignment, you received an error C2666 "2 overloads have similar conversions". Read the help information on this error. What was the compiler unable to decide?

  4. Extra Credit: Why couldn't it decide? Is there any logic that could be used to prefer one choice over the other? Is it better to give an error in this case or to require the programmer to choose the function to call? How could the programmer force the compiler to make a particular decision?

Up

Home Windows C++ 241 C++ 242 Contact Info Contents

Copyright © 2000  Charlie Poole. All rights reserved.
Revised: July 15, 2002 - cpoole@ctc.edu