Question: For this lab you will be creating an auto-expanding dynamic-array. This array will explicitly hold std library strings. As with all labs you can create
For this lab you will be creating an auto-expanding dynamic-array. This array will explicitly hold std library strings. As with all labs you can create any PRIVATE data members/ methods you want, but the Public interface should remain the same. While tests will be provided, you will need to add your own test cases to ensure all corner cases are accounted for and avoided. This class will be used for future labs so it is important that it is tested thoroughly.
The following provides the expected behavior of the private data members and the public interface/API:
private:
std::string * data - pointer to the string data
unsigned length - Current number of strings stored in array
unsigned allocated_length - Current number of strings allocated to be held in the array
public:
stringVector() - Constructor, should construct an empty object
virtual ~stringVector()- Destructor, should deallocate all memory used by object (NO MEMORY LEAKS)
unsigned size()-return the number of strings stored in array
unsigned capacity() -return number of strings currently allocated to be stored in array
void reserve() - Allows user to choose the allocation size, if it is small than current array then data should be truncated to fit
bool empty() - returns true IFF the array is empty
void append(std::string data) - append data to end of array, if capacity is zero set it to 1, otherwise double array capacity if this is over capacity
void swap(unsigned pos1, unsigned pos2) - swap the string in position1 (pos1) of the array with the string in position2, throw an exception if either position is out of bounds
stringVector &operator = (stringVector &rhs) - Copies RHS to object calling the function (this should be a hard COPY, creating a separate object with same values)
std::string& operator[ ](unsigned position) -return a reference to the string at this position, throw an exception if out of bounds
void sort() - use the bubble sort function discussed in lab to sort the vector like a dictionary (lower letters and less letters first)
Header File:
class stringVector { private: std::string *data; unsigned length; unsigned allocated_length; public: stringVector(); virtual ~stringVector(); unsigned size()const; unsigned capacity()const; void reserve(unsigned new_size); bool empty()const; void append(std::string new_data); void swap(unsigned pos1, unsigned pos2); stringVector &operator=(stringVector const &rhs); std::string &operator[](unsigned position); void sort(); }; CPP Implementation File:
stringVector::stringVector() { this->data = nullptr; length = 0; allocated_length = 0; } stringVector::~stringVector() { delete[] data; data = nullptr; } unsigned stringVector::size() const{ return length; } unsigned stringVector::capacity() const{ return allocated_length; } void stringVector::reserve(unsigned new_size) { } bool stringVector::empty() const{ } void stringVector::append(std::string new_data) { } void stringVector::swap(unsigned pos1, unsigned pos2) { } stringVector &stringVector::operator=(stringVector const &rhs) { //return ; } std::string &stringVector::operator[](unsigned position) { //return ; } void stringVector::sort() { }
Hello! I would like help on filling this implementation file with the ways each function should work in the section above the cpp file code. This is in C++.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
