Question
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
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 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 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 dynamicArray to NULL 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, the function then:
- creates a new dynamic array one element smaller than dynamicArray
- copies all elements except the input string into the new array
- deletes dynamicArray
- decrements size
- returns true.
- A function named getEntry that takes an integer as input and returns the string at that index in dynamicArray. It should 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.
Use the client code below to test your object class.
Once you are done, hand in your object class source code.
Client (test) code
// main.cpp
#include
#include "DynamicStringArray.h"
using namespace std;
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("Burns");
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 Burns from names
names.deleteEntry("Burns");
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 Burns from names2
names2.deleteEntry("Burns");
cout << "Copied names:" << endl;
for (int i = 0; i < names3.getSize(); i++)
cout << names3.getEntry(i) << endl;
cout << endl;
return 0;
}
Sample Run
List of names:
Frank
Wiggum
Nahasapeemapetilon
Quimby
Flanders
After adding a name:
Frank
Wiggum
Nahasapeemapetilon
Quimby
Flanders
Spuckler
After removing a name:
Frank
Wiggum
Quimby
Flanders
Spuckler
After removing a name that isn't on the list:
Frank
Wiggum
Quimby
Flanders
Spuckler
After adding another name:
Frank
Wiggum
Quimby
Flanders
Spuckler
Muntz
After removing all of the names:
After adding a name:
Burns
Testing copy constructor
Copied names:
Burns
Testing assignment
Copied names:
Burns
Step by Step Solution
3.54 Rating (164 Votes )
There are 3 Steps involved in it
Step: 1
Program code to copy DynamicStringArraycpp Header files include stdafxh optional include include include using namespace std definition of class Dynam...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started