Lab07, CISC181, Spring 2004

Part 0: Copying files and checking your disk quota

Note: There is stuff to turn in from part 0 this week!

Throughout this lab, you'l be using files from the lab07 subdirectory. You might want to just copy all the files from that subdirectory into your current directory now. Here's a command to do it:

cp -r ~pconrad/public_html/cisc181/04S/labs/lab07 .

This will copy the entire lab07 subdirectory, along with all its contents, into the current directory. You'll then have to cd into lab07 to see all the files. The -r stands for recursive.

(Note: that command was in both labs 6 and 7, so you can certainly expect that it might show up on Midterm 2!)

In addition, you should take this opportunity to check your disk quota on strauss.

Type "quota -v" at the Unix prompt. You'll see some numbers such as the following:

> quota -v
Disk quotas for pconrad (uid 53430):
Filesystem     usage  quota  limit    timeleft  files  quota  limit    timeleft
/scratch           0      1      1                  0      0      0            
/home/usra     12944  20240  20240               3417  76800  76800            
> 

You need to compare the number listed under "usage" with the number listed under "quota". For example, in the listing above, the usage is 12944 (somewhere been 12MB and 13MB) and the quota is 20240 (20MB). This is good; this is about where you want to be.

If on the other hand, your usage is very close to your quota, you may run into lots of difficulties. Compling, editing, logging on and off of the Sun Ray servers—all of these operations may begin to fail or produce strange errors.

In this case, you need to clean up your disk space usage. Here are some useful commands which you should learn (and know for the next exam!)

> cd
> du -sh *

This sequence of commands changes directory to your main directory, and then gives you a list of all your files and directories in your main directory, along with a summary of the disk usage for each file or directory. The -s flag indicates that you will get a summary of all the disk space under a particular directory, while the -h flag indicates that you will get results reported in K or M, standing for Kilobytes (units of 1024 bytes) or Megabytes (units of a bit over a million bytes, 1024 x 1024 to be exact).

A good candidate for deleting is your old "executable" files; a.out files, and any file you create with the -o flag from the compiler. They tend to be large, and once you've produced your script, you don't need them anymore; you can always bring them back any time you like by recompiling. But DON'T get rid of the source files (.cpp, .cc, .h. .dat, etc.) for your old labs; you may need them for future labs this semester! Only delete the exectuable files, and the .o files (ask your TA if you are not sure.)

To turn in: a script file lab07p0.txt in which you run the "quota -v" command and show that your disk usage is at least 3-4MB less than your quota. That's the amount of "headroom" you need in order to effectively do programming work. If your usage is higher than that, use the "du -sh *" command to do some deleting. You do NOT have to script the deleting part; only the "quota -v" command showing the end result of your clean up needs to be in your script.

Part 1: Function Overloading

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, but we need to revisit that now.

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 word 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 lab07 subdirectory mentioned in Part 0 of this lab (filename: fig03_25.cpp). Copy this file into your account and run it.

Then, write your own program that illustrates the principle of function overloading. Call your source code lab07p1.cpp or lab07p1.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:

  1. The first one should take no parameters at all (i.e. it should have the following prototype:
    int readInteger(void);
    

    Inside that function, print a prompt on cout that says "Please enter an integer >". Read a value in, and return that value as the result of the function.
  2. The second version of the readInteger function should take one integer parameter 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.

  3. The third version of the function will take two integer parameters: min and max. The idea is the same as for the second version of the function, except this time, you are checking that the value is between min and max, inclusive. This time, its up to you to come up with the function prototype. Your function should provide an appropriate prompt, issue an appropriate error message if needed, and return only after the user enters a legal value.
  4. 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.
  5. Now, before you script, you are going to split your work into three separate files:

Then make a script lab07p1.txt where you cat your source files, compile them and run the executable to show that it works.

Grading


Phillip T Conrad
Last modified: Thu Apr 15 16:05:49 EDT 2004