In: Computer Science
Description: One problem with dynamic arrays is that once the array is created using the new operator the size cannot be changed. For example, you might want to add or delete entries from the array similar to the behavior of a vector. This assignment asks you to create a class called DynamicStringArray that includes member functions that allow it to emulate the behavior of a vector of strings.
The class should have the following:
• A private member variable called dynamicArray that references a dynamic array of type string. • A private member variable called size that holds the number of entries in the array.
• A default constructor that sets the dynamic array to nullptr and sets size to 0.
• A function that returns size.
• A function named addEntry that takes a string as input. The function should create a new dynamic array one element larger than dynamicArray, copy all elements from dynamicArray into the new array, add the new string onto the end of the new array, increment size, delete the old dynamicArray, and then set dynamicArray to the new array.
• A function named deleteEntry that takes a string as input. The function should search dynamicArray for the string. If not found, it returns false. If found, it creates a new dynamic array one element smaller than dynamicArray. It should copy all elements except the input string into the new array, delete dynamicArray, decrement size, and return true.
• A function named getEntry that takes an integer as input and returns the string at that index in dynamicArray. It should return nullptr if the index is out of dynamicArray’s bounds. • A copy constructor that makes a copy of the input object’s dynamic array.
• Overload the copy assignment operator so that the dynamic array is properly copied to the target object.
• A destructor that frees up the memory allocated to the dynamic array.
#include<bits/stdc++.h>
using namespace std;
class DynamicStringArray{
string* dynamicArray;
int size;
public:
DynamicStringArray(){
dynamicArray = nullptr;
size = 0;
}
int getsize()const {
return size;
}
void addEntry(string data){
string* temp = new string[size + 1];
for(int i=0; i<size; i++){
temp[i] = dynamicArray[i];
}
temp[size] = data;
size++;
delete[] dynamicArray;
dynamicArray = temp;
}
bool deleteEntry(string data){
bool found = false;
int ind ;
for(int i=0; i<size; i++){
if(dynamicArray[i] == data){
found = true;
ind = i;
break;
}
}
if(found){
string* temp = new string[size - 1];
int j = 0;
for(int i=0; i<size; i++){
if(i != ind)
temp[j++] = dynamicArray[i];
}
size--;
delete[] dynamicArray;
dynamicArray = temp;
return true;
}
return false;
}
string getEntry(int ind){
if(ind >= size) return nullptr;
else return dynamicArray[ind];
}
DynamicStringArray(const DynamicStringArray& other){
size = other.getsize();
dynamicArray = new string[size];
for(int i=0; i<size; i++)
dynamicArray[i] = other.dynamicArray[i];
}
void operator= (const DynamicStringArray& other) {
delete[] dynamicArray;
size = other.getsize();
dynamicArray = new string[size];
for(int i=0; i<size; i++)
dynamicArray[i] = other.dynamicArray[i];
}
~DynamicStringArray(){
delete[] dynamicArray;
}
};
int main(){
DynamicStringArray aa ;
aa.addEntry(string("i am xyz"));
aa.addEntry(string("i am saw"));
aa.deleteEntry(string("i am saw"));
cout<<aa.getsize()<<endl;
cout<<aa.getEntry(0);
return 0;
}