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.
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.
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.
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.
Now, write your own program, according to these instructions:
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:
int readInteger(void);
min
, for example:int readInteger(int min);This function should read an integer from cin that is greater than or equal to min and return that value. It should prompt the user with a message like the following: "Please enter an integer >= 2", except that instead of 2, you should print out the value of min that was passed in.
This function should check the value entered by the user; if it is less than min, print an error message, and ask the user to enter the value again. Repeat this process until the user enters a valid value, and then return that value as the result of the function.
CC -c readInteger.cc CC -c lab05c.cc CC readInteger.o lab05c.o -o lab05c
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