Question: C++ Programming Help: Please add the following functions to the code below: 1. Add a constructor to be able to create an empty struct 2.

C++ Programming Help:

Please add the following functions to the code below:

1. Add a constructor to be able to create an empty struct

2. create a push_back function to add elements into the collection. If the collection is empty, it would allocate a one-element array and store a new value in it

3. create a pop_back function to remove elements from the collection. If the collection is empty, throw an underflow exception.

The code below has the base code for storing elements in a collection and adding elements into a collection (dynamic array). Please edit this program to make it function using vectors.

#include #include using namespace std;

// Wrapper class template //TBD = To Be Determined

class TArray { private: TBD* myArray; int size; public: TArray(int size) { if (size <= 0) { throw invalid_argument("Bad Size"); } myArray = new TBD[size]; this->size = size; } ~TArray() { delete[] myArray; } int getSize() { return size; } void set(TBD value, int index) { if (index < 0 || index >= size) { throw overflow_error("index is out of bounds"); } myArray[index] = value; } TBD at(int index) { if (index < 0 || index >= size) { throw overflow_error("index is out of bounds"); } return myArray[index]; } TBD front() { return myArray[0]; } TBD back() { return myArray[size - 1]; } TBD& operator[](int index) { if (index < 0 || index >= size) { throw overflow_error("index is out of bounds"); } return myArray[index]; }

};

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!