to add or delete entries from the alray allow it to emulate the behavior of a ve
ID: 3911567 • Letter: T
Question
to add or delete entries from the alray allow it to emulate the behavior of a vector of strings. Once your class is ready, test it using the main function uploaded to Blackboard. Your class should have: Your task is to create a class called DSA that includes member functions that A private member variable called dynamicArray that references a dynamic array of type string (this should be a pointer variable). 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, return false. If found, create a new dynamic array one element smaller than dynamicArray. 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. Return nulltptr 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 assignment operator so that the dynamic array is properly copied to the target object. Overload the plus (+) operator to act as an alias for the addEntry function. Simply, the user should be able to add new strings to the array as follows myArraymyArray + "new String"; Overload the minus (-) operator to act as an alias for the deleteEntry function Simply, the user should be able to delete strings from the array as follows: myArray = myArray- " some String". A destructor that frees up the memory allocated to the dynamic arrayExplanation / Answer
ScreenShot
---------------------------------------------------------------------------
Program
//Header file for I/O operation and string manipulations
#include<iostream>
#include<string>
using namespace std;
//Create a class DSA
class DSA {
//Member Variables
private:
string *dynamicArray;
int size;
//Member functions
public:
//Default constructor
DSA() {
dynamicArray = nullptr;
size = 0;
}
//enter each elements into array
void addEntry(string val)
{
string * temp = new string[size + 1];
for (int i = 0; i < size; i++)
temp[i] = dynamicArray[i];
temp[size] = val;
size++;
delete[] dynamicArray;
dynamicArray = temp;
}
//Delete elements from the array
bool deleteEntry(string val)
{
for (int i = 0; i < size; i++) {
if (dynamicArray[i] == val) {
string * temp = new string[size - 1];
for (int j = 0; j < size - 1; j++) {
if (dynamicArray[j] != val) {
temp[i] = dynamicArray[j];
}
}
size--;
if (dynamicArray[size - 1] != val) {
temp[size - 1] = dynamicArray[size - 1];
}
delete[] dynamicArray;
dynamicArray = temp;
return true;
}
}
return false;
}
//+ Operator overload
DSA& operator +(string val)
{
string * temp = new string[size + 1];
for (int i = 0; i < size; i++)
temp[i] = dynamicArray[i];
temp[size] = val;
size++;
delete[] dynamicArray;
dynamicArray = temp;
return *this;
}
// - Operator overload
DSA& operator -(string val)
{
for (int i = 0; i < size; i++) {
if (dynamicArray[i] == val) {
string * temp = new string[size - 1];
for (int j = 0; j < size-1; j++) {
if (dynamicArray[j] != val) {
temp[i] = dynamicArray[j];
}
}
size--;
if (dynamicArray[size - 1] != val) {
temp[size - 1] = dynamicArray[size - 1];
}
delete[] dynamicArray;
dynamicArray = temp;
return *this;
}
}
return *this;
}
//Find out the particular index value
string getEntry(int ind) {
if (ind >= size) {
return nullptr;
}
else {
return dynamicArray[ind];
}
}
//Copy constructor
DSA(const DSA&dsa) {
dynamicArray = dsa.dynamicArray;
size = dsa.size;
}
//Overload = operator
DSA& operator=(const DSA& dsa)
{
// check for self-assignment
if (this == &dsa)
return *this;
// first we need to deallocate any value that this string is holding!
delete[] dynamicArray;
// because m_length is not a pointer, we can shallow copy it
size = dsa.size;
// m_data is a pointer, so we need to deep copy it if it is non-null
if (dsa.dynamicArray)
{
// allocate memory for our copy
dynamicArray = new string[size];
// do the copy
for (int i = 0; i < size; ++i)
dynamicArray[i] = dsa.dynamicArray[i];
}
else
dynamicArray = 0;
return *this;
}
//Return size of the array
int getSize() {
return size;
}
//destructor
~DSA() {
delete[] dynamicArray;
}
};
//tester
int main()
{
//Object creation
DSA dsa;
//Display size of the array initially
cout << "Size of array Initially=" << dsa.getSize() << endl;
//do some additon and deletion of data using functions and overloading
dsa.addEntry("hello");
dsa.addEntry("world");
cout << "Size of array after add operation=" << dsa.getSize() << endl;
dsa=dsa-"hello";
cout << "Size of array after -operation=" << dsa.getSize() << endl;
dsa=dsa+"Hello";
cout << "Size of array after + operation=" << dsa.getSize() << endl;
//Get the value of index
cout << "String at the index " << 1 << "=" << dsa.getEntry(1) << endl;
return 0;
}
-------------------------------------------------------------------
Output
Size of array Initially=0
Size of array after add operation=2
Size of array after -operation=1
Size of array after + operation=2
String at the index 1=Hello
Press any key to continue . . .
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.