CS12, Program 3

 

Assigned: February 12th, 2002

 

Due: February 22th, 2002, 11pm

 

 

 

Topics

 

·      Pointers and dynamic arrays

·      Classes and pointers

·      Copy constructors

·      Working in Linux

 

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.

 

 

The Program

 

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

 

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

·      Overload the [] operator to allow “natural” access of array elements.

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

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

·      Initialize all array elements to 0 on declaration.

 

All arrays must be dynamically allocated, and all array access in the class implementation must be done using pointer access.   No credit will be given if array access is done using square brackets.  Of course, since you are overloading the [] operator, work in the main program should use square brackets to access array elements.

 

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.

 

To Do

 

1.    Download the file array.h.  Create array.cc to implement the methods and operators in array.h so that they perform the tasks as described.  Pay close attention to the comments preceding each method and adhere to the specifications.

 

2.    Write a program that tests your class by doing the following:

·      Read a size for the array

·      Declare an array of the input size

·      Print the array using <<

·      Read values to read into the array

·      Print the array using <<

·      Declare a second array and initialize it to the contents of the first using the copy constructor. (Array second = first;)

·      Print the second array using <<

·      Declare a third array object, twice as large

·      Copy the first array into the third using =

·      Print the third array using <<

·      Declare a fourth array object about half the size of the original

·      Attempt to copy the first array into the fourth using =

·      Print the fourth array using <<

·      Attempt to access an out-of-bounds element

·      Attempt to assign a value to an out-of-bounds element

 

The Input

 

An int n to represent array size

n doubles to be input into the array.

 

The Output

 

The output should be similar to this example (amounts will differ depending on user input). 

 

Enter array size

5

 

Declaring array

0 0 0 0 0

 

Enter values for the array cells

4.777 6.9 0 8 2.113

 

Content of cell 1

6.9

 

4.777 6.9 0 8.0 2.113

 

Second array

4.777 6.9 0 8.0 2.113

 

Third array

4.777 6.9 0 8.0 2.113 0 0 0 0 0

 

Fourth array

4.777 6.9

 

Content of cell 50

Error

 

 

 

What to turn in

 

·      array.h

 

·      array.cc

 

·      main.cc

 

 

Please note:

 

·      Do not use any other names for your files.  These names are case sensitive – do not use capital letters anywhere in the file names.  Failure to name your files as above will cost you 10 points. 

 

·      Please make sure that both array.cc and main.cc includes your name, login and lab section.  Each is worth 4 points on this assignment.

 

 

Extra Credit

 

·      Overload the == operator to test if two arrays are the same (same size and same contents).

 

·      Allow the user to determine the start index for the array to be any number rather than 0.  The assignment operator will need to be redone to ensure that all the indices in the copied array have matching indices in the target array.  The == operator, if you did one, will have  to check for start index as well as size and contents.

 

·      Add to your main code that shows the additional functionality of your class.  Do not ask for user input.  Instead, you should hardwire the example into your code.  You must do this to receive any extra credit.