/**************************************** This is the header file for the class DynamicArray which represents a dynamic array of integers Author: Lalka Rieger Date: Novemebr 19, 2014 Known Bugs: None *****************************************/ #include using namespace std; class DynamicArray { public: /* initialize the IntArray to store the numbers given * @param values: the array of int values to be stored into the object * @param values_len: the number of elements in array values */ DynamicArray(int values[], int values_len); /*initializes one DynamicArray object as another * @param source: the DynamicArray that will be the source of * values to be copied for the new object * @postcondition: the invoking object is initialized with the length * and capacity of the source object. The new object will have * all the values stored in the source's array, but will not point * to the same address */ DynamicArray(const DynamicArray& source); /*destructor * Deletes the contents of a Dynamic Array object when it passes out of * scope. It frees the memory the DynamicArray occupied. * @postcondition: the object that passes out of scope will be deleted. * Its dynamic memory will be deleted and returned to the heap. */ ~DynamicArray(); /*overloaded assignment operator * overloads = to assign DynamicArray objects to one another * @param right: the DynamicArray that overwrites the first on the left * @postcondition: invoking object will have all the member values of a2, * except a1 will have an identical array but not the same int pointer */ DynamicArray& operator =(const DynamicArray& right); /*overloads [] operator * returns the integer stored at the selected index * @param index: the index of the element that is accessed from invoking object's array * @precondition: the index is in the bounds of the invoking object's array * @return: the integer element at the index is returned. If the * index is out of bounds, error is reported and * the 0th element is returned. */ int& operator[](int index) const; /*overloaded operator >> * overloads input operator to take input until -1 is entered. * The entered values are saved in the invoking object's array. * The array grows, if necessary. * @param ins: the instream object holding the input * @postcondition: any contents of the invoking object's array * are overwritten. The capacity grows to accomodate the input * in increments of 10. The length is set to the number of * elements inputted. * @return: ins is returned to caller */ friend istream& operator>>(istream& ins, DynamicArray& dyn); /*Default consructor * Automatically called to initialize DynamicArray objects * @postcondition: the invoking object's array pointer is intialized * to point to an array of 20; its capacity is intialized to 20; * its length is initialized to 0 */ DynamicArray(); /*operator + overload *appends two DynamicArray objects together * @param a1: the first object * @param a2: the second object to append * @return: a DynamicArray object will be returned of capacity * a1.capacity+a2.capacity and length of a1.length+a2.length * the array pointer will point to an array containing first the elements * of a1.array, then a2.array */ friend DynamicArray operator +(const DynamicArray& a1, const DynamicArray& a2); /*overload of << operator * displays a DynamicArray's int array in the form {x, y, z, ..., n} */ friend ostream& operator<< (ostream & outs, const DynamicArray & dyn); /*append * adds one int to the end of the invoking object's array * @param apendee: the integer number to be appended to the array * @postcondition: the invoking object's array contains the value of apendee at the end * of the array; length of the invoking object is increased by 1; * if the array is at capacity before apendee is inserted, * the array capacity is incresed by 10 */ void append(int apendee); /*get_length * returns the value of the length of the invoking object * @return: the length of the invoking object's array */ int get_length() const; /*get_capacity * returns the capacity of the invoking object * @return: capacity of the invoking object's array */ int get_capacity() const; private: int capacity;//capacity of the dynamic array int * array;//pointer to the dynamic array(of integer) int length;//number of elements contained in the dynamic array };