My Little Vector
In this homework assignment, you are going to implement your own generalized vector class calledMyVector
. Similar to the STLvector
class, 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 filewhich 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 actualvector
class offers. The following UML diagram shows the members you will need for theMyVector
class 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&
|
TheMyVector
class template also requires an inner class calledVectorError
that 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 any
bad_alloc
errors that might be thrown when using thenew[]
operator
- Just throw the exception object again so that
main
catches it
- For the copy constructor, don't forget to also copy the elements in the copy object's buffer to the current buffer
- The
add
method needs to insert the givenitem
at the end of the vector, which means you will need to allocate new memory since the size will need to increase by 1
- The
back
method returns the last element in the vector
- Throw a
VectorError
object with an appropriate error message if the vector is empty
- The
find
method returns the index number that the givenitem
is stored in for the vector
- Throw a
VectorError
object with an appropriate error message if the vector is empty or ifitem
cannot be found in the vector
- The
front
method returns the first element in the vector
- Throw a
VectorError
object with an appropriate error message if the vector is empty
- The
remove
method needs to delete the givenitem
from the vector, which means you will need to allocate new memory since the size will need to decrease by 1
- Throw a
VectorError
object with an appropriate error message if the vector is empty or ifitem
is not in the vector
- The
reset
method makes the vector have no elements
- The
size
method is just an accessor method for thebufferSize
attribute
- The
operator=
overload copies the state ofotherObj
tothis
(current object)
- Prevent self-assignments
- If the buffer sizes don't match each other, allocate the appropriate memory for
buffer
and avoid memory leaks
- Copy the elements in the other object's buffer to the current buffer
- The
operator[]
overload simply returns the element found in the givenindex
- Throw a
VectorError
object with an appropriate error message if the vector is empty or ifindex
is 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 theMyVector
class. 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