Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Convert the class that you wrote in assignment 2 to template. Test the template with dynamic arrays of integers and dynamic array of strings. ***************

Convert the class that you wrote in assignment 2 to template.

Test the template with dynamic arrays of integers and dynamic array of strings.

***************

Your assignment will be tested with the following main function:

int main() { DynamicArray 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 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 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 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; }

Convert this code (Assignment 2):

#include

#include

#include

using namespace std;

class DynamicStringArray

{

public:

DynamicStringArray();

DynamicStringArray(const DynamicStringArray& otherObj);

~DynamicStringArray();

DynamicStringArray& operator = (const DynamicStringArray& rightSide);

void addEntry(string newEntry);

bool deleteEntry(string newEntry);

string getEntry(int index);

int getSize();

private:

string *dynamicArray;

int size;

};

DynamicStringArray::DynamicStringArray()

{

dynamicArray = NULL;

size = 0;

}

DynamicStringArray::DynamicStringArray(const DynamicStringArray& otherObj)

{

size = otherObj.size;

if (size == 0)

{

dynamicArray = NULL;

}

else

{

dynamicArray = new string[size];

for (int i = 0; i < size; i++)

{

dynamicArray[i] = otherObj.dynamicArray[i];

}

}

}

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;

}

DynamicStringArray:: ~DynamicStringArray()

{

if (dynamicArray != NULL)

delete[] dynamicArray;

}

int DynamicStringArray::getSize()

{

return size;

}

void DynamicStringArray::addEntry(string newEntry)

{

string *newArray = new string[size +1];

for (int i = 0; i < size; i++)

{

newArray[i] = dynamicArray[i];

}

delete[] dynamicArray;

dynamicArray = newArray;

newArray[size++] = newEntry;

}

bool DynamicStringArray::deleteEntry(string entry)

{

string *result = NULL;

int pos = -1;

for (int i = 0; (i < size)&&(pos == -1); i++)

{

if (dynamicArray[i] == entry)

{

pos = i;

}

}

if (pos == -1)

{

return false;

}

if (size > 1)

{

result = new string[size -1];

}

for (int i = 0, j = 0; i < size; i++)

{

if (i != pos)

{

result[j++] = dynamicArray[i];

}

}

size--;

delete[] dynamicArray;

dynamicArray = result;

return true;

}

string DynamicStringArray::getEntry(int index)

{

if((index < 0) || (index >= size))

return NULL;

return dynamicArray[index];

}

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("Nahasaoeemapetilon");

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 aother name:" << endl;

cout << endl;

//Remove all of the names by repeadtedly 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;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Professional Microsoft SQL Server 2012 Administration

Authors: Adam Jorgensen, Steven Wort

1st Edition

1118106881, 9781118106884

More Books

Students also viewed these Databases questions

Question

3 > O Actual direct-labour hours Standard direct-labour hours...

Answered: 1 week ago

Question

What are possible safety concerns? Explain.

Answered: 1 week ago

Question

What would you do if you were in Margarets shoes?

Answered: 1 week ago