Question: C++ This class is very similar to std::vector . It needs to support push_back, at, reserve, capacity, size, and data methods (just like vector does).

C++

This class is very similar to std::vector. It needs to support push_back, at, reserve, capacity, size, and data methods (just like vector does). It also needs to support the Rule of three member functions as well as one additional member function: a constructor that takes a pointer to a dynamically allocated array and an int (denoting the size of that array). The class must use this array for its internal data structure (similar to the Stack example from the lecture). However, if the reserve method is called with a larger capacity than the array has, a new dynamically allocated array should be created.

".h file"

class DynamicVector {

private:

int * array_ = nullptr;

int size_ = 0;

int capacity_ = 0;

public:

DynamicVector(int * array, const int & size);

DynamicVector(const DynamicVector & x);

DynamicVector& operator=(DynamicVector x);

~DynamicVector();

int & at(int index);

};

TEST CASES::

const int size = 3;

int * array = new int [size] {1, 4, 7};

DynamicVector dv(array, size);

int & result = dv.at(0);

ASSERT_EQ(result, 1);

result = 99;

ASSERT_EQ(dv.at(0), 99);

ASSERT_EQ(array[0], 99);

ASSERT_EQ(dv.at(2), 7);

ASSERT_THROW(dv.at(3), std::out_of_range);

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 Mathematics Questions!