ESc 014

Lab 8 -- Sorter Template
Thursday, May 30, 2002

Objectives

Introduction

Templates provide a way for us to generalize our programs so that we don't need one version for each variable type. For example, we studied a version of SelectionSort in class that works with arrays of ints. One would not want to have one version of SelectionSort for ints, one for doubles, one for strings, etc. In this lab, you'll complete a template that lets you write the sort routine just once and then use it for lots of different types.

Like last week, you'll do this lab in the UNIX environment on the blackduck computer.

Setup

Following the same steps you used back in last week's lab, copy the directory named "lab8" to your home directory from the esc014pub directory. (Go back to lab 7 if you need a refresher).

Once you have your own personal copy of the "lab8" directory, you should find two files within it. One is named "sorterTest.cpp". This is the tester file (the one with main) that will use the sorter class you're about to write to sort arrays of anything, be they strings or ints or whatever. I've printed out a hard copy for you so you can study it to see what it's doing.

The second file is named "sorter.h". This is the file you will edit with pico. "sorter.h" will define the sorter class, including the implementation of all the member functions.

Your Mission

Your job is to complete the "sorter.h" file to make the class work.

I have provided the private section of the class for you. It consists of one variable and one member function. The variable called list is an array of type genericType. This is the name of the type parameter you should use in this template class.

Note how I placed the findIndexOfMax function in the private section. This is done as a security measure. By making the function private, only other member functions can call it. The main function can't call it. SelectionSort used findIndexOfMax "internally" to find the next biggest item to swap with. It is only needed by the function doing the sorting, not by any other function. Since no other function needs it, we can protect it from being used unnecessarily by making it private.

Defining the Class

You should first write the code that tells this file to be a template class. The class will be named sorter. Don't forget to include the #ifndef, #define, and #endif keywords in the appropriate places. Leave the named constant SIZE I included outside the class definition (that is, before the #ifndef line).

The Public Section

You'll next be writing the public section of the class. You'll be writing the following three member functions:

Compiling and Debugging

The class should now be complete. You can compile your code by typing:
g++ sorterTest.cpp -o mySorter
Remember, you don't need to compile the header file. The "-o" option tells the compiler to place the finished executable into a file called mySorter. You can run it by typing
./mySorter
Debug your code until it works. Don't forget that you can press Control-C while in pico to display the line number you are on. This makes compiler errors easier to find.

Don't forget to run your own tests too. Don't just depend on mine.

How to turn in this lab

When you are done, you can logout of your blackduck account by typing
logout
Then use the file transfer program to transfer the two files (sorterTest.cpp and sorter.h) to your desktop. Place the files into a folder, name the folder with your name, zip them up (right click on the folder), and then upload the zip file to BlackBoard. Don't forget to print out hard copies and to include your opening comment block!

Remember, you will be graded on the correctness of your implementation, neatness, presentation, style of your program code, thoroughness of your testing, and good use of the C++ language. It's important to comment your code where appropriate and to do little things like space things properly, use readable indentation, and also to make sure the overall design and logic of the program are coherent.


Return to Lab Index