Question: Create your own vector class that has the following properties: 1) A push_back method which adds an item to the end of the vector, expanding

Create your own vector class that has the following properties: 1) A push_back method which adds an item to the end of the vector, expanding the underlying data storage as needed. 2) A pop_back method which returns the last item in the vector and removes it, shortening the length of the vector by 1. 3) A size method which returns how many items are in the vector 4) A capacity method which returns the number of items that the vector can hold before it needs to expand it's underlying storage 5) An ensure_capacity method which sets the size of the underlying data store to a specific length. After ensure_capacity is run, the capacity function should return at LEAST the value passed into ensure_capacity 6) An overloaded Index ([]) operator which allows a user to get an item at a specified index (ex: myvector[3] would get the item at index 3 in the vector)

--++Use C++ templates to ensure that your vector class can accept any data type and Name your class "vector.h" and use the test harness below to test your class

// testVector.cpp #include "vector.h" #include

int main() { vector myIntVector(); vector myDoubleVector(); myIntVector.push_back(5); myIntVector.push_back(10); assert(myIntVector[0]==5); assert(myIntVector.size() == 2); myIntVector.ensure_capacity(10); assert(myIntVector.capacity()>=10); myDoubleVector.push_back(2.0); myDoubleVector.push_back(5.0); double retrieved = myDoubleVector.pop_back(); assert(myDoubleVector.size()==1); assert(retrieved == 5.0); std::cout << "All tests run successfully!" << std::endl; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!