Greetings, I have the following code that needs to be converted from its current
ID: 3749528 • Letter: G
Question
Greetings, I have the following code that needs to be converted from its current class to a template. The function should not be implemented inside the class/template like I have done either, please define the functions outside of the class/template if you can. Thank you ahead of time for your help.
********
Here is the current code I have, the main testing function for it is below my code.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
class Person
{
string name;
vector <Person *> children;
public:
Person()
{
name = "";
}
Person(string name):name(name) // constructor to create a person with name
{
}
string get_name()
{
return name;
}
void setChild(string c)
{
children.push_back(new Person(c));
}
void displayChildren()
{
cout << "Children" << endl;
for(int i = 0; i < children.size(); i++)
cout << children[i]->get_name() << " ";
cout << endl;
}
~Person()
{
children.clear();
}
};
vector<Person> persons;
void displayChildren(string name)
{
for(vector<Person>::iterator it = persons.begin() ; it != persons.end(); ++it)
{
if(!name.compare((*it).get_name()))
(*it).displayChildren();
}
}
********
Here is the testing main function for the code:
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;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
template <class T>
class Person
{
T name;
vector <Person *> children;
public:
Person();
Person(T name):name(name) // constructor to create a person with name
{
}
T get_name();
void setChild(T c);
void displayChildren();
~Person();
};
vector<Person> persons;
void displayChildren(string name)
{
for(vector<Person>::iterator it = persons.begin() ; it != persons.end(); ++it)
{
if(!name.compare((*it).get_name()))
(*it).displayChildren();
}
}
template <class T>
T Person<T>::get_name()
{return name;}
template <class T>
void Person<T>::setChild()
{children.push_back(new Person(c));}
template <class T>
void Person<T>::displayChildren()
{cout << "Children" << endl;
for(int i = 0; i < children.size(); i++)
cout << children[i]->get_name() << " ";
cout << endl;}
template <class T>
Person<T>::~Person()
{children.clear();}
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;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.