Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I need help converting a class code to TEMPLATE, THIS IS THE CLASS CODE BELOW: #include using namespace std ; // DynamicStringArray class class DynamicStringArray{
I need help converting a class code to TEMPLATE, THIS IS THE CLASS CODE BELOW:
#includeusing namespace std; // DynamicStringArray class class DynamicStringArray{ private: //members string *dynamicArray; int size; public : // default constructor DynamicStringArray(){ size = 0; dynamicArray = new string[size]; } // copy constructor DynamicStringArray(const DynamicStringArray &stringArray){ size = stringArray.size; string *newDynamicArray = new string[size]; for (int i = 0; i < size; i++) { newDynamicArray[i] = stringArray.dynamicArray[i]; } dynamicArray = newDynamicArray; } // getSize function int getSize(){ return size; } // addEntry function void addEntry(string entry){ string *newDynamicArray = new string[size+1]; for (int i = 0; i < size; i++) { newDynamicArray[i] = dynamicArray[i]; } newDynamicArray[size] = entry; size++; delete [] dynamicArray; dynamicArray = newDynamicArray; } // deleteEntry function bool deleteEntry(string entry){ string *newDynamicArray = new string[size]; bool found = false; int j = 0; for (int i = 0; i < size; i++){ if (!found && dynamicArray[i] == entry) { found = true; } else { newDynamicArray[j++] = dynamicArray[i]; } } if(found){ size--; delete [] dynamicArray; dynamicArray = newDynamicArray; } return found; } // getEntry function string getEntry(int n){ return dynamicArray[n]; } // overload assignment operator void operator=(const DynamicStringArray &stringArray){ cout << "here" << endl; size = stringArray.size; string *newDynamicArray = new string[size]; for (int i = 0; i < size; i++) { newDynamicArray[i] = stringArray.dynamicArray[i]; } dynamicArray = newDynamicArray; } // destructor ~DynamicStringArray(){ delete [] dynamicArray; } }; ///********************************************************************************** /// main method - tester program ///********************************************************************************** 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;
**PLEASE CONVERT THE CLASS SECTION OF THIS CODE INTO TEMPLATE AND CHANGE THE MAIN FUNCTION TO THIS EXACT FORMAT:
int main() { DynamicArray<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; DynamicArray<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; DynamicArray<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 << "Testing dynamic array of ints" << endl; DynamicArray<int> nums; nums.addEntry(10); nums.addEntry(20); nums.addEntry(30); for (int i = 0; i < nums.getSize(); i++) cout << nums.getEntry(i) << endl; cout << endl; cout << "Enter a character to exit." << endl; char wait; cin >> wait; return 0; }
**so, to sum it up: the final code should include a template version of class DynamicStringArray, and a different main function that is given above (please don't change anything in the given main function, you can just copy and paste it to the new template version code). Please make sure there are no errors when the code runs.
thank you!
Step by Step Solution
There are 3 Steps involved in it
Step: 1
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