/************************************************************************* Author: Dana Vrajitoru, IUSB, CS Class: C243, Fall 2019 File name: MyArray.cc Last updated: August 26, 2019. Description: Implementation of a class that implements a safe array. **************************************************************************/ #include "MyArray.h" #include using namespace std; #include // Constructor with given size, can be used as default constructor. MyArray::MyArray(int theSize) { // initialize the object as empty array = NULL; size = 0; // then use the resize function to allocate the memory resize(theSize); } // MyArray::MyArray() // Destructor. If the array is not empty, it must be deallocated. MyArray::~MyArray() { // call the function empty instead, so we only need to modify in // one place if anything changes empty(); } // MyArray::~MyArray() // Copy constructor: initializes the target object with the size of // the object data and copies the content of the object data into the // target object. MyArray::MyArray(MyArray &data) : size(data.size) // initialize the size { // allocate the array array = new int[size]; // copy all the values from data for (int i=0; i= 0) // check that the new size is valid { if (size > 0) // if the array is not empty, deallocate first empty(); if (theSize != 0) // allocate again if the size is not 0 { size = theSize; array = new int[size]; } } else // negative size, give an error message cout << "Resize attepmted with a negative size. " << "Operation ignored." << endl; } // MyArray::resize() // Access an element of the array. If the index (subscript) is out of // the range of the array, it prints an error message and exits the // program. int &MyArray::operator[](int index) { // check that the index is in the right range if (index >= 0 && index < size) return array[index]; else // error { cerr << "Illegal access to an element of the array." << endl << "The size of the array was " << size << " and the index was " << index << endl; exit(1); } } // MyArray::operator[]() // Returns the size of the array. int MyArray::getSize() { return size; } // MyArray::getSize() // Outputs the elements of the array. void MyArray::output() { // output the size cout << "The array of size " << size << " contains the elements:" << endl; // then all the elements for (int i=0; i