// PersonPr Implementation #include "PersonPr.h" PersonPr::PersonPr(void) { this->name = "Unknown"; this->mother = NULL; this->father = NULL; } PersonPr::PersonPr( const PersonPr & right) { this->name = right.name; if ( right.mother == NULL ) this->mother = NULL; else this->mother = new PersonPr(*(right.mother)); // dereference pointer so you pass use entire object instead of a pointer // this goes 1 level deep into family tree, calls same copy constructor, but // not an infinite loop if ( right.father == NULL) this->father = NULL; else this->father = new PersonPr(*(right.father)); } PersonPr::PersonPr( const string & theName) // mother and father not known { this->name = theName; this->mother = NULL; this->father = NULL; } PersonPr::PersonPr( const string & theName, const string & motherName, const string & fatherName) { this->name = theName; this->mother = new PersonPr(motherName); this->father = new PersonPr(fatherName); } PersonPr::PersonPr( const string & theName, const PersonPr & motherPerson, const PersonPr & fatherPerson) { this->name = theName; this->mother = new PersonPr(motherPerson); this->father = new PersonPr(fatherPerson); } PersonPr::~PersonPr(void) // the destructor - return memory allocated with new { cout << "Object " << name << " is leaving" << endl; // ADD CODE BELOW TO RETURN HEAP MEMORY IF NECESSARY if ( mother != NULL) { delete mother; mother = NULL; } if ( father != NULL) { delete father; father = NULL; } } string PersonPr::getName(void) const { return this->name; } string PersonPr::getMotherName(void) const // if not known, return "UNKNOWN" { if ( mother == NULL ) return "Unknown"; else return mother->getName(); } string PersonPr::getFatherName(void) const // if not known, return "UNKNOWN" { if ( father == NULL ) return "Unknown"; else return father->getName(); } string PersonPr::getMaternalGrandMotherName(void) const // if not known, return "UNKNOWN" { // BEFORE DEREFERENCING A POINTER BE SURE AND CHECK THAT IT IS NOT NULL if ( mother == NULL ) return "Unknown"; else if ( mother->mother == NULL) return "Unknown"; else return mother->mother->getName(); }