In: Computer Science
C++
Hello .I need to convert this code into template and then test the template with dynamic array of strings also if you can help me move the function out of the class that would be great.also There is a bug where the memory was being freed without using new operator. I cant seem to find it thanks in advance
#include
using namespace std;
class DynamicStringArray
{
private:
string *dynamicArray;
int size;
public:
DynamicStringArray(DynamicStringArray &A)
{
dynamicArray=A.dynamicArray;
size=A.size;
}
~DynamicStringArray()
{
delete
[]dynamicArray;
}
DynamicStringArray &operator
=(DynamicStringArray &A)
{
dynamicArray=new
string[A.size];
for(int i=0; i
{
dynamicArray[i]=A.getEntry(i);
}
}
DynamicStringArray()
{
dynamicArray=NULL;
size=0;
}
void addEntry(string input)
{
string *temp=new
string[size+1];
for(int i=0; i
{
temp[i]=dynamicArray[i];
}
temp[size]=input;
size++;
delete
[]dynamicArray;
dynamicArray=temp;
}
bool deleteEntry(string
input)
{
bool
found=false;
for(int i=0; i
{
if(dynamicArray[i]==input)
{
found=true;
break;
}
}
if(!found)
{
return false;
}
else
{
string *temp=new
string[size-1];
int j=0;
for(int i=0; i
{
if(dynamicArray[i]!=input)
{
temp[j]=dynamicArray[i];
j++;
}
}
size--;
delete
[]dynamicArray;
dynamicArray=temp;
}
}
string getEntry(int index)
{
if(index<0 ||
index>=size)
{
cout<<"Index out of range"<
return "";
}
return
dynamicArray[index];
}
int getSize()
{
return
size;
}
};
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;
}
If you have any doubts, please give me comment...
#include <iostream>
using namespace std;
template <class T>
class DynamicStringArray
{
private:
T *dynamicArray;
int size;
public:
DynamicStringArray(DynamicStringArray<T> &A);
~DynamicStringArray();
DynamicStringArray &operator=(DynamicStringArray &A);
DynamicStringArray();
void addEntry(T input);
bool deleteEntry(T input);
T getEntry(int index);
int getSize();
};
template <class T>
DynamicStringArray<T>::DynamicStringArray(DynamicStringArray<T> &A)
{
size = A.size;
dynamicArray = new T[size];
for (int i = 0; i < size; i++)
{
dynamicArray[i] = A.dynamicArray[i];
}
}
template <class T>
DynamicStringArray<T>::~DynamicStringArray()
{
delete[] dynamicArray;
}
template <class T>
DynamicStringArray<T> &DynamicStringArray<T>::operator=(DynamicStringArray &A)
{
dynamicArray = new T[A.size];
for (int i = 0; i < size; i++)
{
dynamicArray[i] = A.getEntry(i);
}
}
template <class T>
DynamicStringArray<T>::DynamicStringArray()
{
dynamicArray = NULL;
size = 0;
}
template <class T>
void DynamicStringArray<T>::addEntry(T input)
{
T *temp = new T[size + 1];
for (int i = 0; i < size; i++)
{
temp[i] = dynamicArray[i];
}
temp[size] = input;
size++;
delete[] dynamicArray;
dynamicArray = temp;
}
template <class T>
bool DynamicStringArray<T>::deleteEntry(T input)
{
bool found = false;
for (int i = 0; i < size; i++)
{
if (dynamicArray[i] == input)
{
found = true;
break;
}
}
if (!found)
{
return false;
}
else
{
T *temp = new T[size - 1];
int j = 0;
for (int i = 0; i < size; i++)
{
if (dynamicArray[i] != input)
{
temp[j] = dynamicArray[i];
j++;
}
}
size--;
delete[] dynamicArray;
dynamicArray = temp;
}
}
template <class T>
T DynamicStringArray<T>::getEntry(int index)
{
if (index < 0 || index >= size)
{
cout << "Index out of range" << endl;
return "";
}
return dynamicArray[index];
}
template <class T>
int DynamicStringArray<T>::getSize()
{
return size;
}
int main()
{
DynamicStringArray<string> 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<string> 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<string> 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;
}