Greetings, I have the test code and a small piece of code for this project but a
ID: 3745118 • Letter: G
Question
Greetings, I have the test code and a small piece of code for this project but am unsure how to complete it. Thank you ahead of time for your help.
This project 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:
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 NULL ( or 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 NULL 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.
A destructor that frees up the memory allocated to the dynamic array.
Here is the operator = overloading function, the test code is at the bottom, below this piece of code.
DynamicStringArray& DynamicStringArray::operator =(const DynamicStringArray& rightSide)
{
if (dynamicArray != NULL)
{
delete[] dynamicArray;
}
if (rightSide.size == 0)
{
size = 0;
dynamicArray = NULL;
}
else
{
size = rightSide.size;
dynamicArray = new string[size];
for (int i = 0; i < size; i++)
{
dynamicArray[i] = rightSide.dynamicArray[i];
}
}
return *this;
}
The program will be tested with the following driver program. The code below shouldn’t need editing.
*********
int main()
{
DynamicStringArray names;
// List of names
names.addEntry("Frank");
names.addEntry("Wiggum");
names.addEntry("Nahasapeemapetilon");
names.addEntry("Quimby");
names.addEntry("Flanders");
// Output list
cout << "List of names:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
// Add and remove some names
names.addEntry("Spuckler");
cout << "After adding a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
names.deleteEntry("Nahasapeemapetilon");
cout << "After removing a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
names.deleteEntry("Skinner");
cout << "After removing a name that isn't on the list:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
names.addEntry("Muntz");
cout << "After adding another name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
// Remove all of the names by repeatedly deleting the last one
while (names.getSize() > 0) {
names.deleteEntry(names.getEntry(names.getSize() - 1));
}
cout << "After removing all of the names:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
names.addEntry("Olivia");
cout << "After adding a name:" << endl;
for (int i = 0; i < names.getSize(); i++)
cout << names.getEntry(i) << endl;
cout << endl;
cout << "Testing copy constructor" << endl;
DynamicStringArray names2(names);
// Remove Olivia from names
names.deleteEntry("Olivia");
cout << "Copied names:" << endl;
for (int i = 0; i < names2.getSize(); i++)
cout << names2.getEntry(i) << endl;
cout << endl;
cout << "Testing assignment" << endl;
DynamicStringArray names3 = names2;
// Remove Olivia from names2
names2.deleteEntry("Olivia");
cout << "Copied names:" << endl;
for (int i = 0; i < names3.getSize(); i++)
cout << names3.getEntry(i) << endl;
cout << endl;
cout << "Enter a character to exit." << endl;
char wait;
cin >> wait;
return 0;
}
Explanation / Answer
Below is your code
Code:
DynamicStringArray.h
//Include libraries
#ifndef DynamicStringArray_H
#define DynamicStringArray_H
#include<string>
//Use namespace
using namespace std;
//Define class
class DynamicStringArray
{
//Define access specifier
private:
//Declare variable
string* dynamicArray;
//Declare variable
int size;
//Define access specifier
public:
//Define method
DynamicStringArray::DynamicStringArray()
{
//Assign value
dynamicArray = NULL;
//Assign value
size = 0;
}
//Define method
int getSize();
//Define method
void addEntry(string element);
//Define method
bool deleteEntry(string input);
//Define method
string getEntry(int k);
//Define method
DynamicStringArray(const DynamicStringArray& other);
//Define method
DynamicStringArray operator==(const DynamicStringArray& other);
//Define destructor
~DynamicStringArray();
};
//End
#endif
DynamicStringArray.cpp
//Include libraries
#include <string>
#include"DynamicStringArray.h"
//Use namespace
using namespace std;
//Define function
int DynamicStringArray::getSize()
{
//Return
return size;
}
//Define method
void DynamicStringArray::addEntry(string element)
{
//Create instance
string* new_dynamic_array = new string[size+1];
//Declare variable
int i;
//Loop
for(i=0; i<size; i++)
//Assign value
new_dynamic_array[i] = dynamicArray[i];
//Assign value
new_dynamic_array[i] = element;
//Increment value
size++;
//Delete
delete[] dynamicArray;
//Assign value
dynamicArray = new_dynamic_array;
}
//Define method
bool DynamicStringArray::deleteEntry(string input)
{
//Declare variable
int i;
//Loop
for(i=0; i<size; i++)
//If condition satisfies
if(dynamicArray[i].compare(input)==0)
//Break
break;
//If condition satisfies
if(i==size)
//Return
return false;
//Create array
string *new_dynamic_array = new string[size-1];
//Declare variable
int index = 0;
//Loop
for(i=0; i<size; i++)
{
//If condition satisfies
if(dynamicArray[i].compare(input)!=0)
//Assign value
new_dynamic_array[index++] = dynamicArray[i];
}
//Delete
delete[] dynamicArray;
//Decrement
size--;
//Assign value
dynamicArray = new_dynamic_array;
//Return
return true;
}
//Define method
string DynamicStringArray::getEntry(int k)
{
//If condition satisfies
if(k<0 || k > size)
//Return
return NULL;
//Return
return dynamicArray[k];
}
//Define method
DynamicStringArray::DynamicStringArray(const DynamicStringArray& other)
{
//Assign value
size = other.size;
//Create instance
dynamicArray = new string[size];
//Loop
for(int i=0; i<size; i++)
//Assign value
dynamicArray[i] = other.dynamicArray[i];
}
//Define method
DynamicStringArray DynamicStringArray::operator==(const DynamicStringArray& other)
{
//Assign value
size = other.size;
//Create instance
dynamicArray = new string[size];
//Loop
for(int i=0; i<size; i++)
//Assign value
dynamicArray[i] = other.dynamicArray[i];
//Return
return *this;
}
//Define destructor
DynamicStringArray::~DynamicStringArray()
{
//Delete
delete[] dynamicArray;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.