Lab08, CISC181, Spring 2005

Part 1: Function Overloading (50 pts)

The topic of "function overloading" is crucial to a discussion of classes, especially when it comes to understanding constructors. When we covered functions, we glossed over section 3.20 in your textbook, but we need to revisit that now. So, please re-read section 3.20 sometime this week.

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.

If possible, review section 3.20 of your textbook before proceeding (if you don't have a textbook handy, you can still proceed with the lab, but you'll find it easier if you get a chance to look over section 3.20). You can find the source code for the program from Figure 3.25 of your text in the lab08 subdirectory (filename: fig03_25.cpp). Copy this file into your account and run it.

Then, 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 lab08a.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 Figure 3.25 from Deitel/Deitel 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 lab08b.cc
    CC readInteger.o lab08b.o -o lab08b
    
  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 "lab08b", and then rules to make lab08b as needed. Also include a "make clean" rule that removes all a.out and *.o files, along with the lab08b executable.
  6. Make a script lab08b.txt where you cat your source files (including your Makefile), compile them and run the executable to show that it works. You do not need to include the lab08a program in your script.
  7. Finally, create a tar file lab08.tar consisting of all your files from this step. (You may omit lab08a.cc or include it; it doesn't matter.) 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.
  8. Upload your lab08.tar and your lab08.txt on WebCT, and print your lab08.txt for your TA. Don't "submit" yet though, you aren't done with this lab yet!

Part 2: Getting Started on Project 2 (50 pts).

As part of your credit for lab08, complete step 1 of project 2. The submission and grading guidelines are the same as for project 2, so please reference those instructions: http://udel.edu/~pconrad/cisc181/05S/work/proj/proj2.html

For this part of the lab, go ahead and work in your project 2 subdirectory. Submit your p2s1.cc and p2s1.txt files, as per the instructions in the project 2 directory.

Grading rubric with sample deductions

Note: these are sample deductions.  They are not the only possible deductions.
Part 1. Overloaded functions			50 pts
   - C++ correctness		15 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			10 pts
     - misleading var names, -2 pts each
   - Correct script		5 pts
     - cat 3 files + Makefile, clean
   - Correct Makefile, tar file		10 pts
     - No .h dependencies, -3 pts
     - tar file contains .o or executables -3
   - Sufficiently test program	10 pts
Part 2. First steps of project 2			50 pts
   - C++ correctness		15 pts   
   - C++ style			10 pts

   - Correct script/submission		15 pts

   - Sufficiently test program	10 pts