Data structure and algorithms
/** * @author Jane Programmer * @cwid 123 45 678 * @class COSC 2336, Spring 2019 * @ide Visual Studio Community 2017 * @date February 15, 2019 * @assg Assignment 07 Operator Overloading * * @description Assignment 07 part 01, practice with operator overloading. * In this first part of assignment, you need to define a ListType class * and overload the indicated operators. This version of your class * will only support list of int values. You will turn this into a * class template in part 2 of the assignment. */ #include "ListType.hpp" // static member variables have to be initialized like this int ListType::nextListId = 1; /** default constructor * Initialize as an empty list. Initially we have no memory * allocated, and the size (and allocation size) are 0. */ ListType::ListType() { id = nextListId++; size = allocSize = 0; item = NULL; } /** constructor (empty) * Initialize as an empty list with indicated iniaial * size of memory allocated. * * @param allocSize The initialze size for the empty list. */ ListType::ListType(int allocSize) { id = nextListId++; size = 0; this->allocSize = allocSize; item = new int[this->allocSize]; } /** constructor (array) * Initialize a list using an array of items for the initial values * in the list. * * @param size The number of items in the array given for initialization. * @param items An array (pointer to base address) of items to initialize * this list with. */ ListType::ListType(int size, int* initItem) { id = nextListId++; this->size = size; this->allocSize = size; item = new int[this->size]; // copy the items into this list for (int index = 0; index < this->size; index++) { item[index] = initItem[index]; } } /** destructor * The class destructor. Be good stewards of memory and make * sure that we free up memory allocated to hold our list items * by this object when it goes out of scope. We display some * information for debugging/tracking ListType destruction. */ ListType::~ListType() { cout << "ListType: <id=" << id << "> out of scope, size: " << size << " allocSize: " << allocSize << endl; // be a good memory manager, free up memory we have allocated if (item != NULL) { delete [] item; } } /** overload operator= * Overload the operator= assignment operator. Whenever one list * variable is assigned to another this operator is invoked. * * @param rhs The list on the right hand side of the assignment, the * contents of which is to be (deep) copied to this list contents. * * @returns ListType Returns a reference to this list, after contents * have been copied/assigned. */ const ListType& ListType::operator=(const ListType& rhs) { // only assign if not doing a self-assignment if (this != &rhs) { // copy the values from rightList into this list int newAllocSize = rhs.size; // if not enough space, grow our list if (this->allocSize < newAllocSize) { int* newItem = new int[newAllocSize]; delete [] item; item = newItem; this->allocSize = newAllocSize; } // copy the items from righ hand side into this list for (int index = 0; index < rhs.size; index++) { this->item[index] = rhs.item[index]; } this->size = rhs.size; } // return the object assigned return *this; }