Lab05, CISC181, Spring 2006

Function Overloading

We will soon be starting the topic of classes in C++.

The topic of "function overloading" is crucial to a discussion of classes, especially when it comes to understanding constructors. As a result, you should re-read (or read for the first time?!?) section 4.2 in your Savitch textbook.

I'm not kidding: read section 4.2 in Savitch!

Even if you have a solid understanding of the "concept" of functions from a previous course (say, in C, or Java), there are some details about function overloading in C++ that deserve careful review. Function overloading is a concept not present in C, so you didn't learn it in CISC105.

What is function overloading?

The idea of function overloading is similar to the idea of "multiple senses of a word" in the English language. Consider the uses of the word "rose" in the following three sentences:

In one sentence, "rose" refers to a flower. In another "rose" is the past tense of "rise", and in the third, "rose" refers to a color. The word "rose" by itself is ambiguous—capable of having multiple meanings. We "disambiguate" the word by looking at the surrounding context.

In the same way, a given "name" in C++ can refer to different functions. We can "disambiguate" the name by looking at context. The main idea of "function overloading" is that in C++ (unlike in plain C), a programmer may include more than one function with exactly the same name in the same program. This is called "overloading" the function name. So, for example, you can define two (or more) different functions that are both named "squared". However, each of the versions of the function must have different parameters. This is how the compiler can distinguish between the different functions when you make a function call. Again, the technical term is "disambiguate"; the compiler is "removing the ambiguity" that is created by having multiple functions with the same name, by using context: in this case, the number and type of parameters.

Later we'll also talk about "operator overloading", which is related to function overloading.

Step 1: Compile and Run minValue.cc, minValue2.cc (20 pts)

Copy the program minValue.cc into your lab05 directory, compile it and run it. Then read through the source code, and understand what is happening. If you have questions, ask your TA. (Questions about the source code and how it works are not dumb questions—they indicate that you are smart enough not to skip steps in your lab assignments. Your TA will appreciate that.

Next, compile minValue2.cc with the CC compiler and then with the g++ compiler. You should get an error message in both cases. Understand why this error message is happening, and write a file called lab05a.txt (just a plain text file that you open up in emacs) in which you write a couple of sentences that explain why you got that particular error message. Also indicate two different ways you could fix the program so that it works (I can think of at least four different ways, but I'm only asking you to describe two of them. :-) )

Then, choose one of those ways, and fix the program, and save the result back to the file minValue2.cc.

Step 2: Write a program that illustrates operator overloading (80 pts)

Now, write your own program, according to these instructions:

  1. Start by writing a simple program that illustrates the principle of function overloading. Call your source code lab05b.cc. Your program should include a main function, and three functions, each of which is named readInteger. Each of the functions should return an integer, but they will differ in the number of types of parameters:

  2. Once you have written the three functions, write a main program that will test these three functions, and demonstrate that each of them works properly. If you aren't sure what is expected, you can use minValue.cc as a model.
  3. Now, before you script, you are going to split your work into three separate files:
  4. Now, compile your files separately, as follows (you may substitute g++ for CC)
    CC -c readInteger.cc
    CC -c lab05c.cc
    CC readInteger.o lab05c.o -o lab05c
  5. Now, create a Makefile for this project that automates the compiling of those three steps. Your Makefile should include a rule "all:" that indicates that "all" depends on the exectuables minValue2, lab05b and lab05c, and then rules to make those executables as needed.

    Also include a "make clean" rule that removes all a.out and *.o files, along with the lab05c executable.

Finishing and Submitting

  1. Make a script lab05b.txt where you cat all your source files (including your Makefile), do a make clean, and make all, and run each of the executables to show they work.
  2. Finally, create a tar file lab05.tar consisting of all your files from this lab. Be sure to do a "make clean" before you make the tar file so that you don't end up with a larger tar file than necessary.
  3. Upload your lab05.tar and your lab05a.txt and lab05b.txt on WebCT and submit, and print your lab05a.txt and lab05b.txt for your TA.

Grading rubric with sample deductions

Note: these are sample deductions.  They are not the only possible deductions.


Step 1: 20 pts  
   -- Correct lab05a.txt file per instructions: 10 pts
   -- Correct minValue2.cc file per instructions: 10 pts


Step 2. Overloaded functions			80 pts
   - C++ correctness		20 pts
     - readInteger.h 3 prototypes  3 pts
       - no ifndef in .h, -2 pts
     - readInteger.cpp 3 functions 9 pts
     - main.cpp calls 3 functions  3 pts
   - C++ style			20 pts
     - misleading var names, -2 pts each
   - Correct script		10 pts
     - cat 3 files + Makefile, clean
   - Correct Makefile, tar file		20 pts
     - No .h dependencies, -3 pts
     - tar file contains .o or executables -3
   - Sufficiently complete test program	10 pts