In: Computer Science
Create a family tree with a 3D vector in C++. Then, determine the worst, average and best time complexities of the program.
#include <stdlib.h>
#include <string>
#include <iostream>
#include <vector>
using namespace std;
class Person
{
protected:
string name;
int ID;
string gender;
public:
Person (string name, int ID, string gender)
{
this-> name = name;
this-> ID = ID;
this-> gender = gender;
}
const string getName ()
{
return name;
}
const int getID ()
{
return ID;
}
const string getGender ()
{
return gender;
}
vector <string> children;
};
class Male: public Person
{
public:
Male (string name, int ID, string gender) : Person(name, ID, gender)
{
}
void addWife (Person *arr [], string p1, string p2);
void displayWifeChildren (Person *arr[], string p1)
{
int SIZE = 10;
int i = 0;
int j = 0;
cout << "Children of " << p1 << "\n";
while (i < wife.size () )
{
cout << i + 1 << " " << wife [i] << "\n";
j = 0;
for(int k = 0; k < SIZE; k++)
{
if(arr [k] -> getName() == wife[i])
{
while (j < arr [k] -> children.size () )
{
cout << "\t" << arr [k] -> children [j] << "\n";
j++;
}
}
}
i++;
}
}
private:
vector <string> wife;
};
class Female: public Person
{
public:
Female (string name, int ID, string gender) : Person(name, ID, gender)
{
}
void addChild (Person *arr [], string p1, string p2, string p3);
private:
vector <string> husband;
};
void displayAll (Person *arr[])
{
int SIZE = 10;
cout << "\nThe people in the system are...\n";
for(int i = 0; i < SIZE; i++)
{
cout << arr[i] -> getName () << " " << arr [i] -> getID () << " " << arr [i] -> getGender () << "\n";
}
cout << "\n";
}
void Male:: addWife (Person *arr [], string p1, string p2)
{
int SIZE = 10;
for (int i = 0; i < SIZE; i++)
{
if(arr[i]-> getName () == p1)
{
wife. push_back (p2);
}
}
}
void Female:: addChild (Person *arr[], string p1, string p2, string p3)
{
int SIZE = 10;
for (int i = 0; i < SIZE; i++)
{
if(arr[i]-> getName () == p2)
{
children. push_back (p3);
}
}
}
/*
void displayChildren (Person *arr[], string p1)
{
int SIZE = 10;
for (int i = 0; i < SIZE; i++)
{
if (arr [i] -> getName () == p1)
{
cout << "Children of " << arr [i] -> getName () << " are ";
((Male *) arr [i]) -> displayWifeChildren (arr);
}
}
cout << "\n";
}
*/
int main()
{
const int SIZE = 10;
Person *arr[SIZE];
arr [0] = new Male ("Abraham", 1, "M");
arr [1] = new Female ("Sarah", 2, "F");
arr [2] = new Female ("Keturah", 3, "F");
arr [3] = new Female ("Hagar", 4, "F");
arr [4] = new Male ("Issac", 5, "M") ;
arr [5] = new Male ("Jokshan", 6, "M");
arr [6] = new Male ("Midian", 7, "M");
arr [7] = new Male ("Ishmael", 8, "M");
arr [8] = new Female ("Rebekah", 9, "F");
arr [9] = new Male ("Esau", 10, "M");
displayAll(arr);
Male *m = (Male *) arr [0];
m -> addWife(arr, "Abraham", "Sarah"); /// Sarah is the wife of Abraham
m -> addWife (arr, "Abraham", "Keturah");
m -> addWife (arr, " Abraham ","Hagar");
Female *f1 = (Female *) arr [1];
f1 -> addChild(arr, "Abraham", "Sarah", "Issac"); /// Issac is the child of Abraham and Sarah
Female *f2 = (Female *) arr [2];
f2 -> addChild(arr, "Abraham", "Keturah", "Jokshan");
f2 -> addChild(arr, "Abraham", "Keturah", "Midian");
Female *f3 = (Female *) arr [3];
f3 -> addChild(arr, "Abraham", " Hagar", " Ishmael");
m-> displayWifeChildren(arr, "Abraham");
/*
displayChildren(arr, "Abraham"); /// Displays all children of Abraham and groups each children with a wife
*/
cout << "\nPress enter (or Ctrl-C) to quit ... " << endl;
cin.get();
for (int i = 0; i < 10; i++)
{
if ( arr [i] != NULL)
{
delete arr[i];
}
}
return 0;
}