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
".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
Get step-by-step solutions from verified subject matter experts
