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
// 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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started