Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem: Write the Vector.hpp file based on the Main.cpp file. The output should look the same as the sample output. File Name: Vector.hpp #ifndef VECTOR_HPP

Problem: Write the Vector.hpp file based on the Main.cpp file. The output should look the same as the sample output. File Name: Vector.hpp 
#ifndef VECTOR_HPP #define VECTOR_HPP /** * @brief this class derives from runtime_error * It is thrown in the case of Non-postive matrix dimensions */ class IndexOutOfBoundsException : public std::runtime_error { public: // DO NOT MODIFY THIS CLASS IndexOutOfBoundsException() : std::runtime_error("vector index out of bounds") {// no other code in the constructor } }; /** * @brief Vector is a templated sequence container that uses dynamic size arrays. */ template class Vector { private: T * elements; // an array of T, accessed by a pointer std::size_t capacity; // amt of storage allocated std::size_t size; // current number of elements // we are going to use an initial_size that is ridiculously small // in order to test the ability to resize static const std::size_t initial_size = 4; public: typedef T* iterator; // allows us to use 'iterator' as a type typedef const T* const_iterator; /** * @brief Default Constructor. */ Vector(){ capacity = initial_size; elements = new T [initial_size]; size = 0; } /** * @brief Copy Constructor. * @param other The vector from which we should copy all data members */ Vector(const Vector &other) { // similar to operator= but with fewer lines of code // and no 'if' statement needed } /** * @brief Copy Assignment Operator. * @param other the vector on the RHS of the assignment. * @return A reference to the vector on the LHS. */ Vector &operator=(const Vector &other) { if (this != &other) { // this avoids deletion of elements capacity = other.capacity; size = other.size; delete[] elements; elements = new T[capacity]; // TODO use a for loop to copy the data from other.elements into elements return *this; } } /** * @brief Destructor. */ ~Vector() { std::cout << "Destructor called" << std::endl; // leave this here ! // TODO: add one more line of code to prevent a memory leak } /** * @brief Gets the size of this vector * @return The number of elements in the container. */ size_t Size() const { // TODO: write this code } /** * @brief Checks if the container is empty. * @return true if the container is empty, false otherwise. */ bool Empty() const { // TODO: write this code } /** * @brief Gets the capacity of this vector * @return The number of elements that can be held in currently allocated storage. */ size_t Capacity() const{ // TODO: write this code } /** * @brief Overloaded array subscripting operator without bounds checking * @param pos The position of the element to access. * @return A reference to the element at specified location pos. */ T &operator[](std::size_t index) { return elements[index]; } /** * @brief Const overload of the overloaded array subscripting operator. * @param pos The position of the element to access. * @return A const reference to the element at specified location pos. */ const T &operator[](std::size_t index) const { return elements[index]; } /** * @brief Removes the last element of the container if it exists. * The capacity of the vector is unchanged. * If there are no elements in the container, then do nothing. */ void Pop_Back() { // hint: just modify size // TODO: write this code } /** * @brief Begin iterator. * @return An iterator to the first element. */ iterator Begin() { return elements; } /** * @brief End iterator. * @return An iterator to one past the last element. */ iterator End() { // TODO: write this.... just need one line of code } /** * @brief Begin iterator. * @return A const iterator to the first element. */ const_iterator Begin() const { return elements; } /** * @brief End iterator. * @return A const iterator to one past the last element. */ const_iterator End() const { // same as the other End() function // TODO: write this code } /** * @brief Adds an element to the end. * If there is no space to add an element, * capacity of the vector is doubled * and data and memory are managed * @param elem The element to be added. */ void Push_Back(T thing){ if (size < capacity){ elements[size] = thing; size++; } // TODO: complete this code else{ } } /* @brief Removes all elements from the container. * Leaves the capacity of the vector unchanged */ void Clear() { size = 0; } /** * @brief Erases an element at pos and updates size * Assume that the iterator is valid for this vector * @param pos iterator to the element to remove. * Hint: Use pointers to slide the array items down * You do not need to use delete or new */ void Erase(iterator posToDelete) { // hint: use pointers to slide the array items to the left // you do not need to use delete or new // TODO: write this code } /** * @brief Access specified element with bounds checking. * @param pos The position of the element to access. * @return A reference to the element at specified location pos, with bounds checking. * @throw IndexOutOfBoundsException if out of range */ T &At(std::size_t pos) { // TODO: you need to add a check to throw something here return elements[pos]; } /** * @brief Access specified element with bounds checking. * @param pos The position of the element to access. * @return A const reference to the element at specified location pos, with bounds checking. * @throw IndexOutOfBoundsException if out of range */ const T &At(std::size_t pos) const { // TODO: same as the non-const version } /** * @brief standard friend function for operator<< * @param os the stream you are calling << with * @param v the vector you are trying to output */ friend std::ostream& operator<<(std::ostream& os, const Vector &v) { // do not change this function....it will affect your output ! for (int i=0; i < v.size; ++i){ os << v.elements[i] << ", "; } return os; } }; #endif 
File name: Main.cpp #include #include "Vector.hpp" int main() { std::cout << "testing constructor and Push_Back" << std::endl; Vector v1; v1.Push_Back(9); v1.Push_Back(8); v1.Push_Back(7); std::cout << "v1: " << v1 << std::endl; std::cout << "testing copy constructor v2(v1)" << std::endl; Vector v2(v1); v2.Push_Back(6); std::cout << "adding a 6 onto v2" << std::endl; std::cout << "v2: " << v2 << std::endl; std::cout << "v1: " << v1 << std::endl << std::endl; std::cout << "testing Push_Back with capacity change on v2" << std::endl; for (int i=11; i < 17; ++i){ v2.Push_Back(i); std::cout << "size: " << v2.Size() << " capacity: " << v2.Capacity(); std::cout << " v2: " << v2 << std::endl; } std::cout << std::endl; std::cout << "testing v1[0] = v2[v2.Size()-1]" << std::endl; v1[0] = v2[v2.Size()-1] ; std::cout << "v2: " << v2 << std::endl; std::cout << "v1: " << v1 << std::endl; std::cout << "testing copy constructor v4(v2)" << std::endl; Vector v4(v2); // copy constructor std::cout << "v4: " << v4 << std::endl; std::cout << "v2: " << v2 << std::endl; Vectorv3; v3 = v1; std::cout << "testing v3=v1" << std::endl; // pop until empty std::cout << "v3: " << v3 << std::endl; std::cout << "v4 = v4 causes no problems" << std::endl; v4 = v4; std::cout << "v4: " << v4 << std::endl; std::cout << "popping v3 until empty" << std::endl; while (! v3.Empty() ){ v3.Pop_Back(); std::cout << " after Pop_Back, v3: " << v3 << std::endl; } std::cout << "popping an empty vector then pushing 1" << std::endl; v3.Pop_Back(); v3.Push_Back(1); std::cout << "v3: " << v3 << std::endl; /********* use this for your own testing....iterator values vary from machine to machine for (auto it = v2.Begin(); it != v2.End(); ++it){ std::cout << "iterator: " << it ; std::cout << " points to : " << *it << std::endl; } */ std::cout << std::endl; std::cout <<"testing iterators on v2" << std::endl; std::cout << "v2: " << v2 << std::endl; auto beginIterator = v2.Begin(); std::cout << "begin iterator points to : " << *beginIterator << std::endl; auto endIterator = v2.End(); std::cout << "end iterator points to : " << *endIterator << std::endl; std::cout << "testing const iterators" << std::endl; const Vector v9(v4); // v9 must be accessed with a const iterator std::cout << v9 << "\t" << *(v9.Begin()) << "\t" << *(v9.End()) << std::endl; std::cout << std::endl; std::cout << "testing Size and Clear" << std::endl; std::cout << "size of v2 is " << v2.Size() << std::endl; std::cout << "Clear just sets the size to 0" << std::endl; v2.Clear(); std::cout << "v2: " << v2 << std::endl; std::cout << "size of v2 is " << v2.Size() << std::endl << std::endl; std::cout << "testing Erase" << std::endl; std::cout << "v4: " << v4 << std::endl; auto it = v4.Begin(); ++it; v4.Erase(it); std::cout << "erasing the 2nd item in v4" << std::endl; std::cout << "v4: " << v4 << std::endl; std::cout <<"erasing the last item in v4" << std::endl; it = v4.End(); --it; v4.Erase(it); std::cout << "v4: " << v4 << std::endl << std::endl; std::cout << "testing At()" << std::endl; std::cout << "testing v4.At(3) = 99; v4.At(0) = v4.At(5)" << std::endl; v4.At(3) = 99; v4.At(0) = v4.At(5); std::cout << "v4: " << v4 << std::endl << std::endl; std::cout << "trying v4.At(-1)" << std::endl; try { v4.At(-1) = 100; } catch (IndexOutOfBoundsException e){ std::cout << e.what() << std::endl; } std::cout << "trying v4.At(v4.Size())" << std::endl; try { v4.At(v4.Size()) = 12345; } catch (IndexOutOfBoundsException e){ std::cout << e.what() << std::endl; } std::cout << "trying v4.At()" << std::endl; try { v4.At(1000) = v4.At(9999); } catch (IndexOutOfBoundsException e){ std::cout << e.what() << std::endl; } std::cout << std::endl; std::cout << "testing Vector" << std::endl; Vector d1; d1.Push_Back(3.14); d1.Push_Back(2.71); d1.Push_Back(1.62); d1.At(1) = d1.At(2); std::cout << "d1 = " << d1 << std::endl << std::endl; // two other Vector objects { Vector v5(v4); Vector d2; d2 = d1; } std::cout << "when variables go out of scope, the Destructor is automatically called" << std::endl; return 0; } 

File Name: Sample Output

testing constructor and Push_Back v1: 9, 8, 7, testing copy constructor v2(v1) adding a 6 onto v2 v2: 9, 8, 7, 6, v1: 9, 8, 7, testing Push_Back with capacity change on v2 size: 5 capacity: 8 v2: 9, 8, 7, 6, 11, size: 6 capacity: 8 v2: 9, 8, 7, 6, 11, 12, size: 7 capacity: 8 v2: 9, 8, 7, 6, 11, 12, 13, size: 8 capacity: 8 v2: 9, 8, 7, 6, 11, 12, 13, 14, size: 9 capacity: 16 v2: 9, 8, 7, 6, 11, 12, 13, 14, 15, size: 10 capacity: 16 v2: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, testing v1[0] = v2[v2.Size()-1] v2: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, v1: 16, 8, 7, testing copy constructor v4(v2) v4: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, v2: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, testing v3=v1 v3: 16, 8, 7, v4 = v4 causes no problems v4: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, popping v3 until empty after Pop_Back, v3: 16, 8, after Pop_Back, v3: 16, after Pop_Back, v3: popping an empty vector then pushing 1 v3: 1, testing iterators on v2 v2: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, begin iterator points to : 9 end iterator points to : 0 testing const iterators 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, 9 0 testing Size and Clear size of v2 is 10 Clear just sets the size to 0 v2: size of v2 is 0 testing Erase v4: 9, 8, 7, 6, 11, 12, 13, 14, 15, 16, erasing the 2nd item in v4 v4: 9, 7, 6, 11, 12, 13, 14, 15, 16, erasing the last item in v4 v4: 9, 7, 6, 11, 12, 13, 14, 15, testing At() testing v4.At(3) = 99; v4.At(0) = v4.At(5) v4: 13, 7, 6, 99, 12, 13, 14, 15, trying v4.At(-1) vector index out of bounds trying v4.At(v4.Size()) vector index out of bounds trying v4.At() vector index out of bounds testing Vector d1 = 3.14, 1.62, 1.62, Destructor called Destructor called when variables go out of scope, the Destructor is automatically called Destructor called Destructor called Destructor called Destructor called Destructor called Destructor called

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

3. What should a contract of employment contain?

Answered: 1 week ago

Question

1. What does the term employment relationship mean?

Answered: 1 week ago