CS12, Extra Credit Program

 

Assigned: May 23rd, 2002

 

Due: June 10th, 2001, 11pm

 

 

This is an extra credit program. Depending on the work you put into it, it could be worth up to 0.75 of a letter grade.  Exceptional programs may earn more. This is your opportunity to make up points lost on earlier assignments or on the tests.

 

 

Topics

 

·      Building classes

·      Operator overloading

·      Templates

·      Putting it all together

 

 

Background

 

The array type in C++ is rather rudimentary.  Among other problems, it does not protect the user from exceeding the array bounds; indexing of array elements must start at 0; arrays cannot be assigned to each other directly with an assignment statement; the contents of an entire array cannot be printed out with one cout statement.  In this assignment, you will create an array class that addresses these issues.

 

To do

 

Write a class for an enhanced array class for storing ints that will expand the capabilities of C++ arrays to include the following:

·      Prohibits the user from accessing out of bounds array elements (issues an error statement and does nothing).

·      Allows indexing of the array to start and end at any int value (start must come before end).

·      Accesses array elements naturally using [ ] with the appropriate index.

·      Overloads the = operator to allow assigning one array to another of equal or longer length.

·      Overloads the << operator to allow a nicely formatted output of the contents of the entire array.

 

Implementation of your class is entirely up to you.  You may use one- or two-dimensional arrays, linked lists, or any other implementation that you come up with.  Consider your design carefully before you start coding.  Explain clearly what you are doing and how.  Document everything in a separate file that will be handed in with your program.

 

After you have written your class, you will need to test it.  Write a test main() that illustrates the use of each of the methods of your class at least once, to make sure that all the features work correctly.  Please do NOT create a menu-driven main().

 

What to turn in

 

·      README – a text file containing an explanation of your design and implementation. PLEASE no Word documents.

·      array.h – header file for the Array class

·      array.cpp – implementation file for the Array class

·      testarray.cpp – main program that shows the use of all program features.

 

 

Extra Extra Credit

 

Convert your class into a template class.  This will allow the Array to contain any built-in type or object. 

 

There are some problems with template instantiation in the g++ compiler.  The workaround is to combine array.h and array.cpp into one file. Call the file array.h and #include it in testarray.cpp.  If you do this part of the program, you do not need to turn in the file array.cpp.

 

A Note on Grading

 

You may add as many features as you want to your Array class.  There are many bells and whistles that could be added to such a class to enhance its usability.  The more you do, the more points you can earn.  Grading is relative, not absolute.  This means that the people who do more will get more.

 

However, please keep in mind the following points:

 

1.     If your name isn’t on it, you didn’t do it

2.     If a method is not documented, it isn’t there

3.     If it’s not tested in the main, it wasn’t done

4.     If it doesn’t compile, it will not be graded

 

Yes, this is harsh.  And it would be a shame if you put in a lot of work and end up with no credit.  With this in mind, you would do good to follow these suggestions:

 

1.     Put your name in the program files before you start coding.

2.     Document as you go along

3.     Whenever you complete a method, test it.

4.     Take an incremental approach to turnin.  When you have something, anything, however trivial, that compiles and runs – turn it in.