My Little Vector In this homework assignment, you are going to implement your own generalized vector class called MyVector . Similar to the STL vector class, your implementation will be able to store...

1 answer below »

My Little Vector


In this homework assignment, you are going to implement your own generalized vector class calledMyVector. Similar to the STLvectorclass, your implementation will be able to store elements, increase and decrease its size as needed, and work for any data type that supports its operators.


Downloadthe starting code file

Download the starting code file
which contains the main driver code.



MyVector


To avoid making this homework too complex, we are only going to implement a subset of the methods that the actualvectorclass offers. The following UML diagram shows the members you will need for theMyVectorclass template:

UML for MyVector











MyVector
- bufferSize : int
- buffer : T*

+ MyVector()
+ MyVector(a : int)
+ MyVector(copyObj : const MyVector&)


+ virtual ~MyVector()


+ add(item : T) : void
+ back() const : T
+ find(item : T) const : int
+ front() const : T
+ remove(item : T) : void
+ reset() : void
+ size() const : int


+ operator=(otherObj : const MyVector&) : MyVector&
+ operator[](index : int) const : T&



TheMyVectorclass template also requires an inner class calledVectorErrorthat will be thrown when any errors occur within the class. The UML diagram is relatively simple and its implementation straightforward:

UML for VectorError











VectorError
- e : string
+ VectorError(s = "" : string)
+ what() const : string

The following are implementation notes:



  • Make sure to catch anybad_allocerrors that might be thrown when using thenew[]operator

    • Just throw the exception object again so thatmaincatches it



  • For the copy constructor, don't forget to also copy the elements in the copy object's buffer to the current buffer

  • Theaddmethod needs to insert the givenitemat the end of the vector, which means you will need to allocate new memory since the size will need to increase by 1

  • Thebackmethod returns the last element in the vector

    • Throw aVectorErrorobject with an appropriate error message if the vector is empty



  • Thefindmethod returns the index number that the givenitemis stored in for the vector

    • Throw aVectorErrorobject with an appropriate error message if the vector is empty or ifitemcannot be found in the vector



  • Thefrontmethod returns the first element in the vector

    • Throw aVectorErrorobject with an appropriate error message if the vector is empty



  • Theremovemethod needs to delete the givenitemfrom the vector, which means you will need to allocate new memory since the size will need to decrease by 1

    • Throw aVectorErrorobject with an appropriate error message if the vector is empty or ifitemis not in the vector



  • Theresetmethod makes the vector have no elements

  • Thesizemethod is just an accessor method for thebufferSizeattribute

  • Theoperator=overload copies the state ofotherObjtothis(current object)

    • Prevent self-assignments

    • If the buffer sizes don't match each other, allocate the appropriate memory forbufferand avoid memory leaks

    • Copy the elements in the other object's buffer to the current buffer



  • Theoperator[]overload simply returns the element found in the givenindex




    • Throw aVectorErrorobject with an appropriate error message if the vector is empty or ifindexis outside the subscript range



Sample Run


In the starting code that was provided, test cases have already been written out for all the methods in theMyVectorclass. If you implemented everything correctly, you should get the following output:


vec1 size: 0
Vector is empty, cannot call front() method!
Vector is empty, cannot call back() method!
Vector is empty, cannot use [] operator!
Vector is empty, cannot call find(T) method!
Vector is empty, cannot call remove(T) method!
vec1 size: 1
vec1 front: 15
vec1 back: 15
15
vec2 size: 5
a b c d e
Given index is out of range!
a b c d e
a b c d e f g h i j
vec3 size: 10
Given item cannot be found in find(T) method!
Index of item d is 3
b c d e f g h i
vec3 size: 0
9.8 0.078 -4 354.96 1.1111
3.5 1.1 -2.2 0 4.4 -5.5 0
3.5 1.1 -2.2 0 4.4 -5.5 0
3.5 1.1 -2.2 0 4.4 -5.5 0
3.5 1.1 -2.2 0 4.4 -5.5 0 4.1847
1.1 -2.2 0 4.4 -5.5 0




Answered 14 days AfterApr 28, 2022

Answer To: My Little Vector In this homework assignment, you are going to implement your own generalized vector...

Arun Shankar answered on May 04 2022
89 Votes
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here