Computer Science 210 Lab 3:  Classes and Graphics

Due:  February 15, 1999

Objectives and Overview:  During the rest of the course our primary focus will be building data structures using the Class construct.  The main purpose of this lab is for you to get used to creating and using classes.  In addition the lab introduces the use of simple graphics in Code Warrior.

In the course folder you will find a file arrays.cpp which contains starter code for this lab.  The code implements a program similar to the program in lab 1, except that the array is constantly kept in sorted order and the contents of the array are graphically displayed as the numbers are read in.  Your goals for the lab are twofold.  First, you should implement a new array class called vector which is an improvement over standard C++ arrays.  There are a number of ways in which C++ arrays can be improved, including array bounds checking and initialization of values.  Second, and related to the first, you should make general improvements to the program as a whole.  For example, you might only display parts of the array where values have been filled in, you might perform some error checking, etc.  When you are done your program should represent a significant improvement over the arrays.cpp code.
 

Step 1:  Create a Graphics Project

Copy the arrays.cpp file to the desktop in the usual way.  Within Code Warrior you will want to make a new project.  When you are creating a project, in the very last step when you would normally select the Std C++ Console, instead select the C++ Graphics Console.  This will make a number of useful graphical routines available to you.  In the future anytime you wish to use graphics you should select this Console.
 

Step 2:  Run the Program

Once you've created the project and added arrays.cpp to it, try running the program to get a feel for its behavior.  Can you "break" it?  Are there obvious improvements to make?  Make sure you have a thorough understanding of the code.
 

Step 3:  Add the Vector Class

The main goal of this lab is to replace the global array currently used in the program with an improved version implemented as a class.  You will need to creat a class to serve this purpose.  At a minimum your vector class should do the following:

  1. Include all of the "normal" functionality of arrays - access, deletion, etc.  To do this you will at least need to overload the [ ] operator, in addition to the usual constructor and destructor functions associated with any class.
  2. Include error checking to ensure that no out of bounds memory calls are made.
  3. Provide a standard initialized value when the array is created.
  4. Provide an Output function to display the output.
  5. Provide a Length function which returns the size of the array.
A number of other improvements are possible.  For example, a Resize function could allow the size of the array to be changed dynamically.  Overloading the = operator would allow for direct copying.  It is also quite reasonable to convert the DrawArray() function (already in the program) into a vector class function.  Such a function might additionally take parameters as to where to draw the array on the screen.

This is a good example of a program where you probably want to use your own include file.  Define the vector class and all of its corresponding functions in a file called vector.h and then include that file in your main program.  This way you can use this class in any future program in which you use arrays.
 

Step 4:  Integrate the Vector Class into the program

Once you have written your new class, update the program to include it.  Include other changes to improve the functionality of the program.
 

Lab 3 Deliverables

As with all labs, turn in hardcopy to me and place a copy of the program in the Dropbox (with the usual naming conventions).  Meeting the minimum requirements of the assignment will get you an excellent score on this lab, but not a 100.  To get a top score you will need to make some or all of the extra improvments suggested.  Or come up with other improvements of your own.  A good program should be well commented, readable, robust, and should like pretty nice when you run it.