Question: HW10: Project OOP Dynamic array based class Part 1 Build a class dynListType based on dynamic array. Its declaration is provided below: // no more
HW10: Project OOP Dynamic array based class
Part 1
Build a class dynListType based on dynamic array. Its declaration is provided below:
| // no more MAX class dynListType { public:
private: int* dataPtr; int size; int maxSize; }; |
Follow example in Unit 6 PPT 1 (ch12-ch13 Classes with Dynamic Data), add:
a (default) constructor with an int param (default value 5) and
a destructor.
Add those 10 functions from HW9 into dynListType. Be sure to change each occurrence of dataArr (HW9) into dataPtr. Make additional necessary changes.
getSize()
getMaxSize()
isEmpty()
search()
at()
two inserts
remove()
operator +
operator << (output insertion operator)
At this point your program should be able to compile, but may crash during execution. Thats because copy constructor and overloaded assignment are required to handle dynamic data members.
Now add:
Copy constructor
Overloaded assignment operator =
Your program should work perfectly now. dynListType provides the same functionality listType (HW9) does, except that dynListType doesnt have any capacity cap (i.e. there is no MAX for maxSize).
Test your class thoroughly before proceeding to Part 2.
Part 2
Modify your dynListType program from Part 1.
Overload == operator (comparison operator, as in if (obj1 == obj2) ): return true if two list objects contain the same data items in a same order. Values of maxSize dont matter. For example, given obj1 {maxSize 10, <10, 20, 30>} and obj2 {maxSize 5, <10, 20, 30>}, the comparison should return true. Given obj3 {maxSize 5, <20, 10, 20, 30>}, (obj1 == obj3) should be false.
Modify the two insert functions so a list will never be full, like how Java ArrayList and the STL vector class work. Whenever an insertion reaches the maxSize cap, double maxSize, and reallocate storage space. Here is the suggested pseudo code:
| If size before insertion is equal to maxSize // current maxSize reached maxSize <- 2 * maxSize allocate a new dynamic array with maxSize slots copy data pointed by dataPtr over to new array delete the dynamic array currently pointed by dataPtr let dataPtr point to the newly allocated dynamic array End if
Insert as normal |
Modify your driver accordingly to test the new/modified functions. Here is the output of my sample driver. I hard-coded data in the driver and generate some data using loops. You may do it differently, as long as it tests EACH new/modified function.
Additional requirements for Part 1 and Part 2
Organize your program into header file (interface), implementation file, and driver file. Use #include guard with the header file.
Pre- and Post- condition comments for each new/updated function. Pre- condition may be skipped if the pre-condition is just xx is initialized (not requiring any specific values) for value or const reference parameters. Such a prerequisite will be enforced by compiler.
Comment your program appropriately. Pay attention to the standard stuff like coding style, indention, heading, and curly braces.
The referenced HW_9 is as follows
| #include #include #include #include #include #include using namespace std;
const int MAX = 100; // max capacity for all listType objects class listType { public: listType(int max = 5); // constructor // Post: maxSize <-- max. if max is not specified or <=0, default value 5 will be used. if max > MAX, MAX will be used // size <-- 0. int getSize() const { return size; } // return # of elements actually stored int getMaxSize() const { return maxSize; } // return capacity bool isEmpty() const { return size == 0; } bool isFull() const { return size == maxSize; } int search(int element) const; // look for an item. return index of first occurrence int at(int index) const; // return element at a specific location void print() const; // print content of list on screen in format of [a, b, c] (like what ArrayList in Java does) bool insert(int element); // append/insert an element at the end bool insert(int index, int element); // insert an element into location index. // Shifts the element currently at that index (if any) and any subsequent elements to the right bool remove(int index); // remove element at the specified location private: int dataArr[MAX]; // static array storing data items int size; // actual # of elements stored. size <= maxSize int maxSize; // The capacity of this listType obj. 0 <= maxSize <= MAX. public: file:///C/Users/1125083869C/AppData/Local/Temp/HW9_Barrera%20(Part%202).cpp.txt[9/21/2018 3:25:39 AM] listType* operator +(const listType* l) const { int size1=this->size,size2=l->getSize(); int nsize=size1+size2; listType* templ=new listType(nsize); for(int i=0;i { templ->insert(this->at(i)); } for(int i=0;i { templ->insert(l->at(i)); } return templ; } friend ostream &operator << ( ostream &output, const listType *l ) { int sizen=l->getSize(); output<<"[ "; for(int i=0;i output << l->at(i)<<" "; output<<"]"; return output; } }; listType::listType(int max) { this->maxSize=max; } int listType::search(int element) const { for(int i=0;i { if(dataArr[i]==element) return i; } return -1; } int listType::at(int index) const { if(index>=0&&index { return dataArr[index]; } else std::cout<<"Invalid index accessed"< } bool listType::insert(int element) { if(size file:///C/Users/1125083869C/AppData/Local/Temp/HW9_Barrera%20(Part%202).cpp.txt[9/21/2018 3:25:39 AM] { dataArr[size++]=element; return true; } else { return false; } } bool listType::insert(int index, int element) { if(index>=0&&index<=size) { if(size { int temp=dataArr[index]; dataArr[index]=element; for(int i=index;i { int temp1=dataArr[i+1]; dataArr[i+1]=temp; temp=temp1; } dataArr[size++]=temp; return true; } else { return false; } } else return false; } bool listType::remove(int index) { if(index>=0&&index { for(int i=index;i { dataArr[i]=dataArr[i+1]; } size--; return true; } else return false; } int main() { listType *l1=new listType(5); listType *l2=new listType(5); l1->insert(5); l1->insert(0,15); file:///C/Users/1125083869C/AppData/Local/Temp/HW9_Barrera%20(Part%202).cpp.txt[9/21/2018 3:25:39 AM] l1->insert(10); l1->insert(20); l1->remove(1); l1->remove(0); l2->insert(1000); l2->insert(20); l2->insert(0,100); l2->insert(10); l2->remove(0); l2->insert(30,10); listType* l=*l1+l2; std::cout< return 0; } |
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
