Write a template-based class that implements a template-based
implementation of Homework 3 that allows for any type dynamic
arrays (replace string by the template in all instances
below).
• 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 and
sets size to 0.
– A function named getSize 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.
HomeWork 3
#include <iostream>
#include <string>
using namespace std;
class DynamicStringArray
{
private:
string* dynamicArray;
int size;
public:
DynamicStringArray();
DynamicStringArray(string* dynamicArray, int size);
DynamicStringArray(const DynamicStringArray ©);
DynamicStringArray& operator =(const DynamicStringArray& rgtSide);
~DynamicStringArray();
void setDynamicArray(string* dynamicArray);
void setSize(int size);
string* getDynamicArray();
int getSize();
void addEntry(string input);
bool deleteEntry(string input);
string getEntry(int index);
void output();
};
int main() {
DynamicStringArray version1;
DynamicStringArray version2;
string* strArr;
string entry, repeat;
int size, num, index;
while(size <= 0) { cout << "how long do you want the
array's size to be? "<< endl; cin >> size;
}
cin.ignore();
strArr = new string[size];
for(int i = 0; i < size; i++)
{
cout << "String " << i << ": ";
getline(cin, strArr[i]);
}
version1.setDynamicArray(strArr);
version1.setSize(size);
do {
cout << "If you want to add Entry press 1" <<
endl;
cout << "If you want to delete Entry press 2" << endl;
cout << "If you want to get Entry press 3" << endl;
cout << "If you want to copy the whole array press 4" << endl;
cout << "Your choice is: " << endl;
cin >> num;
cin.ignore();
cout << endl;
if(num == 1)
{
cout << "Entry you want to add to array: " << endl;
getline(cin, entry);
version1.addEntry(entry);
version1.output();
}
else if(num ==
2)
{
cout << "Entry you want to delete from array: " << endl;
getline(cin, entry);
if(version1.deleteEntry(entry))
cout << "This Entry has been deleted" << endl;
else
cout << "Out Of Range" << endl;
version1.output();
}
else if
(num == 3)
{
cout << "Prss a number within
the array's size range to look at it's index: " << endl;
cin >> index;
cin.ignore();
cout << "The Entry is : " << version1.getEntry(index) << endl;
version1.output();
}
else if
(num == 4)
{
version2 = version1;
cout << "The copy version of your array is : " << endl;
version2.output();
version2.~DynamicStringArray();
}
cout << endl;
cout << "press ENTER to continue and 0 to exit : " << endl;
getline(cin, repeat);
}
while(repeat != "0");
version1.~DynamicStringArray();
}
DynamicStringArray::DynamicStringArray()
{
dynamicArray = NULL; size = 0;
}
DynamicStringArray::DynamicStringArray(string* arr, int s)
{
dynamicArray = arr; size = s;
}
DynamicStringArray::DynamicStringArray(const DynamicStringArray
©)
{
dynamicArray = new string;
dynamicArray = copy.dynamicArray;
size = copy.size;
}
DynamicStringArray& DynamicStringArray::operator =(const
DynamicStringArray& rgtSide)
{
if(this == &rgtSide)
return *this;
else {
size = rgtSide.size;
delete [] dynamicArray;
dynamicArray = new string[size];
for(int i = 0; i < size; i++)
dynamicArray[i] = rgtSide.dynamicArray[i];
return *this;
}
}
DynamicStringArray::~DynamicStringArray()
{
delete [] dynamicArray;
dynamicArray = NULL;
}
void DynamicStringArray::setDynamicArray(string* arr)
{
dynamicArray = arr;
}
void DynamicStringArray::setSize(int s)
{
size = s;
}
string* DynamicStringArray::getDynamicArray()
{
return dynamicArray;
}
int DynamicStringArray::getSize()
{
return size;
}
void DynamicStringArray::addEntry(string input)
{
string* temp;
temp = new string[size+1];
for(int i = 0; i < size; i++)
{
temp[i] = dynamicArray[i];
}
temp[size] = input;
dynamicArray = temp;
size++;
}
bool DynamicStringArray::deleteEntry(string input)
{
for(int i = 0; i < size; i++)
{
if(dynamicArray[i] == input)
{
for(int j = i; j < size-1; j++)
{
dynamicArray[j] = dynamicArray[j+1];
}
size--;
return true;
}
}
return false;
}
string DynamicStringArray::getEntry(int index)
{
if(index >= size)
return "NULL";
else
return dynamicArray[index];
}
void DynamicStringArray::output()
{
for(int i = 0; i < size; i++)
{
cout << "String " << i << ": " << dynamicArray[i] << endl;
}
}
//Modified c++ code
#include <iostream>
#include <string>
using namespace std;
template<typename T>
class DynamicStringArray
{
private:
T* dynamicArray;
int size;
public:
DynamicStringArray();
DynamicStringArray(T* dynamicArray, int size);
DynamicStringArray(const DynamicStringArray<T> ©);
DynamicStringArray& operator =(const DynamicStringArray<T>& rgtSide);
~DynamicStringArray();
void setDynamicArray(T* dynamicArray);
void setSize(int size);
T* getDynamicArray();
int getSize();
void addEntry(T input);
bool deleteEntry(T input);
T getEntry(int index);
void output();
};
template<typename T>
DynamicStringArray<T>::DynamicStringArray()
{
dynamicArray = NULL; size = 0;
}
template<typename T>
DynamicStringArray<T>::DynamicStringArray(T* arr, int s)
{
dynamicArray = arr; size = s;
}
template<typename T>
DynamicStringArray<T>::DynamicStringArray(const
DynamicStringArray<T> ©)
{
dynamicArray = new T;
dynamicArray = copy.dynamicArray;
size = copy.size;
}
template<typename T>
DynamicStringArray<T>&
DynamicStringArray<T>::operator =(const
DynamicStringArray<T>& rgtSide)
{
if(this == &rgtSide)
return *this;
else {
size = rgtSide.size;
delete [] dynamicArray;
dynamicArray = new T[size];
for(int i = 0; i < size; i++)
dynamicArray[i] = rgtSide.dynamicArray[i];
return *this;
}
}
template<typename T>
DynamicStringArray<T>::~DynamicStringArray()
{
delete [] dynamicArray;
dynamicArray = NULL;
}
template<typename T>
void DynamicStringArray<T>::setDynamicArray(T* arr)
{
dynamicArray = arr;
}
template<typename T>
void DynamicStringArray<T>::setSize(int s)
{
size = s;
}
template<typename T>
T* DynamicStringArray<T>::getDynamicArray()
{
return dynamicArray;
}
template<typename T>
int DynamicStringArray<T>::getSize()
{
return size;
}
template<typename T>
void DynamicStringArray<T>::addEntry(T input)
{
T* temp;
temp = new T[size+1];
for(int i = 0; i < size; i++)
{
temp[i] = dynamicArray[i];
}
temp[size] = input;
dynamicArray = temp;
size++;
}
template<typename T>
bool DynamicStringArray<T>::deleteEntry(T input)
{
for(int i = 0; i < size; i++)
{
if(dynamicArray[i] == input)
{
for(int j = i; j < size-1; j++)
{
dynamicArray[j] = dynamicArray[j+1];
}
size--;
return true;
}
}
return false;
}
template<typename T>
T DynamicStringArray<T>::getEntry(int index)
{
if(index >= size)
return "NULL";
else
return dynamicArray[index];
}
template<typename T>
void DynamicStringArray<T>::output()
{
for(int i = 0; i < size; i++)
{
cout << "Value " << i << ": " << dynamicArray[i] << endl;
}
}
int main() {
DynamicStringArray<string> version1;
DynamicStringArray<string> version2;
string* strArr;
string entry, repeat;
int size, num, index;
while(size <= 0) { cout << "how long do you want the array's size to be? "<< endl; cin >> size;
}
cin.ignore();
strArr = new string[size];
for(int i = 0; i < size; i++)
{
cout << "String " << i << ": ";
getline(cin, strArr[i]);
}
version1.setDynamicArray(strArr);
version1.setSize(size);
do {
cout << "If you want to add Entry press 1" << endl;
cout << "If you want to delete Entry press 2" << endl;
cout << "If you want to get Entry press 3" << endl;
cout << "If you want to copy the whole array press 4" << endl;
cout << "Your choice is: " << endl;
cin >> num;
cin.ignore();
cout << endl;
if(num == 1)
{
cout << "Entry you want to add to array: " << endl;
getline(cin, entry);
version1.addEntry(entry);
version1.output();
}
else if(num == 2)
{
cout << "Entry you want to delete from array: " << endl;
getline(cin, entry);
if(version1.deleteEntry(entry))
cout << "This Entry has been deleted" << endl;
else
cout << "Out Of Range" << endl;
version1.output();
}
else if
(num == 3)
{
cout << "Prss a number within
the array's size range to look at it's index: " << endl;
cin >> index;
cin.ignore();
cout << "The Entry is : " << version1.getEntry(index) << endl;
version1.output();
}
else if
(num == 4)
{
version2 = version1;
cout << "The copy version of your array is : " << endl;
version2.output();
version2.~DynamicStringArray();
}
cout << endl;
cout << "press ENTER to continue and 0 to exit : " << endl;
getline(cin, repeat);
}
while(repeat != "0");
version1.~DynamicStringArray();
}
Get Answers For Free
Most questions answered within 1 hours.