Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

Database Systems A Practical Approach To Design Implementation And Management

Authors: THOMAS CONNOLLY

6th Edition

9353438918, 978-9353438913

More Books

Students also viewed these Databases questions

Question

How to reverse a Armstrong number by using double linked list ?

Answered: 1 week ago