A Dynamic Array Class

Design and write a class to represent an abstract data type (ADT) for a dynamic array of integers. A dynamic array is an array that can change its size according to the indexing operations needed. It should never be possible to index past the end of the array. Your class should be able to pass the tests provided in the main function listed below. Look at the comments in the code to help design the class appropriately.

Procedure

  1. Think how your array will change its size when needed to. This will involve dynamic memory allocation using the oeprator new.
  2. Add all the necessary ADT methods. Use the BigNum example in chapter 13 to help, as well as chapter 9.
  3. Write and compile your class declaration with the main function given below.
  4. Run the program to verify the working of your class.
  5. Print the results of the program run, as well as the source code of the whole program.

Hints

Make sure you have a full complement of special function members: overloaded constructors, copy constructor, destructor, overloaded assignment.

You will need to overload the index operator [] for normal array indexing. It should return a reference to the element indexed , so that it can be used on the left of an assignment.

You will need to overload the insertion operator (<<) for output streams (see page 383 for an example).

Don't forget, when implementing object copying, to copy whatever any member pointers in your class point to.

Avoid memory leaks, dangling pointers, and shared memory. See chapter 13 for examples of these.

Deliverables

  1. A printout of your entire program source code.
  2. A printout of your program running on the given main function.

Due Date

Hand in your solution by 5pm on Wednesday, April 16th.

The main function

Your class must work with this function, without alteration (this will be mailed to you):

int main() {
  DynArray a1;	// create an empty array (no elements)
  DynArray a2(10);	// create an array of 10 elements, all zero
  DynArray a3(5, 20); // create an array of 5 elements,
// all value 20 cout << "a2 is " << a2 << endl; // print the default values in a2 for (int ind = 0; ind < 20; ind++) a2[ind] = a3[ind] * ind; // put values in the first // 20 elements of a2 DynArray a4 = a2; // a4 is created as a copy of a2 cout << "a4 is " << a4 << endl; // print a4 a1 = a2; // a1 now contains the same elements as a2 cout << "a1 is " << a1 << endl; // print a1 a1[100] = a2[0] * a2[1]; // access elements in a2, and assign
// a value to an element of a1 cout << a1{100] << " from " << a2[0] << " * " << a2[1] << endl; } // a1, a2, a3 and a4 are destroyed when
// the main function exits