Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

FOR THIS PROBLEM IN PARTICULAR, I'M HAVING TROUBLE WITH THE INSERT, EXPAND, PUSH_BACK, RESIZE AND ASSIGN FUNCTIONS. PLEASE HELP ME ON THOSE IN PARTICULAR. THANK

FOR THIS PROBLEM IN PARTICULAR, I'M HAVING TROUBLE WITH THE INSERT, EXPAND, PUSH_BACK, RESIZE AND ASSIGN FUNCTIONS. PLEASE HELP ME ON THOSE IN PARTICULAR. THANK YOU IntVector class, C++

You are going to build a smaller, simpler version of the STL vector class that is capable of only storing integers. Appropriately, it will be called IntVector. This class will encapsulate a dynamically allocated array of integers.

You are required to come up with a header file (IntVector.h) that declares the class interface and a separate implementation file (IntVector.cpp) that implements the member functions. You will also create a test harness (main.cpp) that you use to test each function you are currently developing.

Encapsulated (Private) Data Fields

unsigned sz: stores the size of the IntVector (the number of elements currently being used).

unsigned cap: store the size of the array

int *data: stores the address of the dynamically-allocated array of integers

Public Interface (Public Member Functions)

IntVector()

IntVector(unsigned size, int value = 0)

~IntVector()

unsigned size() const

unsigned capacity() const

bool empty() const

const int & at(unsigned index) const

int & at(unsigned index)

void insert(unsigned index, int value)

void erase(unsigned index)

const int & front() const

int & front()

const int & back() const

int & back()

void assign(unsigned n, int value)

void push_back(int value)

void pop_back()

void clear()

void resize(unsigned size, int value = 0)

void reserve(unsigned n)

You may NOT add any functions to the public interface nor any member variables at all, public or private.

Private Helper Functions

void expand()

void expand(unsigned amount)

You will not be implementing iterators. On the most part, though, the above functions should work just like the STL vector class member function with the same name. Please see the following pages to get a better idea of what each function should do: STL Vector

Constructors and Accessors

IntVector() - the Default constructor

This function should set both the size and capacity of the IntVector to 0 and will not allocate any memory to store integers. (Make sure you do not have a dangling pointer.)

IntVector(unsigned size, int value = 0)

Sets both the size and capacity of the IntVector to the value of the parameter passed in and dynamically allocates an array of that size as well. This function should initialize all elements of the array to the value of the 2nd parameter.

unsigned size() const

This function returns the current size (not the capacity) of the IntVector object, which is the values stored in the sz data field.

unsigned capacity() const

This function returns the current capacity (not the size) of the IntVector object, which is the value stored in the cap data field.

bool empty() const

Returns whether the IntVector is empty (the sz field is 0).

const int & at(unsigned index) const

This function will return the value stored in the element at the passed in index position.

This function should throw an out_of_range exception, passing it the string "IntVector::at_range_check" if the index is beyond the size of the IntVector. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

const int & front() const

This function will return the value stored in the first element.

const int & back() const

This function will return the value stored in the last element.

Destructor, Mutators, & Private Helper Functions

~IntVector()

The destructor is used to clean up (delete) any remaining dynamically-allocated memory. For this class, that will be the array pointed to by the int pointer called data.

void expand()

This function will double the capacity of the vector. This function should reallocate memory for the dynamically allocated array and update the value of capacity. Be careful to properly handle the case when capacity is 0 before calling expand().

Make sure you don't create a memory leak here.

void expand(unsigned amount)

This function will expand the capacity of the vector by the amount passed in. This function should reallocate memory for the dynamically allocated array and update the value of capacity.

Make sure you don't create a memory leak here.

void insert(unsigned index, int value)

This function inserts data at position index. To do this, all values currently at position index and greater are shifted to the right by 1 (to the element right after its current position).

Size will be increased by 1. However, If size will become larger than capacity, this function needs to first double the capacity. In other words, if capacity and size are both 20 when this function is called, this function must first increase the capacity to 40 and then it will be able to increase the size to 21.

Since other functions will also potentially need to expand (double) the capacity, call the private helper function named expand (see above) to do this for you.

This function should throw an out_of_range exception, passing it the string "IntVector::insert_range_check" if the index is too large. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

void erase(unsigned index)

This function removes the value at position index and shifts all of the values at positions greater than index to the left by one (to the element right before its current position).

Size is decreased by 1.

This function should throw an out_of_range exception, passing it the string "IntVector::erase_range_check" if the index is too large. See bottom of specifications for more details on how to throw an exception and how to test that it works properly.

void push_back(int value)

This function inserts a value at the back end of the array.

Size will be increased by 1. However, If size will become larger than capacity, this function needs to first increase the capacity. In other words, if capacity and size are both 20 when this function is called, capacity must first be increased to 40 and then size can be increased to 21.

Since other functions will also potentially need to expand (double) the capacity, call the private helper function named expand (see above) to do this for you.

void pop_back()

This function just needs to decrease size by 1.

This function will never thrown an exception. Calling pop_back on an empty vector will cause undefined behavior.

void clear()

This function reduces the size of the vector to 0.

void resize(unsigned size, int value = 0)

This function resizes the vector to contain size elements.

If size is smaller than the current size(sz), this function just reduces the value of sz to size.

If size is greater than the current size(sz), then the appropriate number of elements are inserted at the end of the vector, giving all of the new elements the value passed in through the 2nd parameter (value).

If the new value of size will be larger than capacity, then the capacity must first be expanded by either doubling (expand()) or by increasing the capacity by a specific amount (expand(size - cap)), whichever results in the largest capacity. Then, this function can then increase the size appropriately.

void reserve(unsigned n)

This function requests that the capacity (the size of the dynamically allocated array) be set to n at minimum. This informs the vector of a planned increase in size, although notice that the parameter n informs of a minimum, so the resulting capacity may be any capacity equal or larger than this.

This function should NOT ever reduce the size of the vector. If n is larger than the current capacity then the capacity should be expanded by either doubling (expand()) or by increasing the capacity by a specific amount (expand(n - cap)), whichever results in the largest capacity.

In any case, a call to this function never affects the elements contained in the vector, nor the vector size.

void assign(unsigned n, int value)

Assigns new content to the vector object, dropping all the elements contained in the vector before the call and replacing them by those specified by the parameters. The new value of size will be n and all values stored in the vector will have the value of the 2nd parameter.

If the new value of size will be larger than capacity, then this function must first expand capacity by either double (expand()) or by increasing the capacity by a specific amount (expand(n - cap)), whichever results in the largest capacity.

int & at(unsigned index)

This function does exactly the same thing the accessor (const) version of at does.

int & front( )

This function does exactly the same thing the accessor (const) version of front does.

int & back( )

This function does exactly the same thing the accessor (const) version of back does.

Throwing an exception

You will need to include the standard library stdexcept. Then, when the index is out of range, execute the following statement:

throw out_of_range("IntVector::at_range_check"); 

To test if this worked, declare an IntVector of size 10 and then call the at function passing it an index argument of 10 or larger. You should see the following output:

terminate called after throwing an instance of 'std::out_of_range' what(): IntVector::at_range_check Aborted 

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

Explain any four (4) consequences of incorporation of a company .

Answered: 1 week ago

Question

Is there a clear hierarchy of points in my outline?

Answered: 1 week ago