Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement the Person class to work as intended. You\'ll need to define the const

ID: 3567731 • Letter: I

Question

Implement the Person class to work as intended. You'll need to define the constructor and two member functions as you would in the .h file e.g. Person & Person::divorce() { ... }.
Hints available on request.

#include <string >
#include <iostream>
using namespace std;
class Person {
private :
Person * spouse;
public:
Person(const string & nom);
Person & marry(Person & p);
Person & divorce() ;

const string  name ;
};


Here is main() along with the expected output .
int main() {
Person p("Allen"), q("Beth"), r("Cathy"), t("David");
p.marry(p);
p.divorce();
p.marry(q);
p.marry(q);
q.marry(p);
p.marry(r);
p.divorce().marry(r);
q.divorce();
r.divorce().marry(t).divorce().marry(q);
return 0;
}
Expected Output :

Silly Allen. You can't marry yourself!
Allen is not married, so there's no need to divorce.
Allen and Beth are now married!
Allen is already married to Beth.
Beth is already married to Allen.
Allen will need to divorce Beth first.
Allen is now divorced from Beth.
Allen and Cathy are now married!
Beth is not married, so there's no need to divorce.
Cathy is now divorced from Allen.
Cathy and David are now married!
Cathy is now divorced from David.
Cathy and Beth are now married!

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

class Person {
private:
   Person * spouse;
public:
   Person(const string & nom);
   Person & marry(Person & p);
   Person & divorce();

const string name;
};

Person::Person(const string & nom):name(nom){

}

Person & Person::marry(Person & p){
   if (spouse != NULL){
       if (spouse->name == p.name){
           cout << name << " is already married to " << p.name << "." << endl;
       }
       else{
           cout << name << " will need to divorce " << spouse->name << " first." << endl;
       }
   }
   else if (p.name == name){
       cout << "Silly " << name << ". You can't marry yourself!" << endl;
   }
   else {
       if (p.spouse != NULL){
           if (p.spouse->name == name){
               cout << p.spouse->name << " is already married to " << name << "." << endl;
           }
           else{
               cout << p.name << " will need to divorce " << p.spouse->name << " first." << endl;
           }
       }
       else{
           spouse = &p;
           p.spouse = this;
           cout << name << " and " << p.name << " are now married!" << endl;
       }
   }
   return *this;
}

Person & Person::divorce(){
   if (spouse == NULL){
       cout << name << " is not married, so there's no need to divorce." << endl;
   }
   else{
       string n = spouse->name;
       spouse->spouse = NULL;
       spouse = NULL;
       cout << name << " is now divorced from " << n << "." << endl;
   }
   return *this;
}

int main() {
   Person p("Allen"), q("Beth"), r("Cathy"), t("David");
   p.marry(p);
   p.divorce();
   p.marry(q);
   p.marry(q);
   q.marry(p);
   p.marry(r);
   p.divorce().marry(r);
   q.divorce();
   r.divorce().marry(t).divorce().marry(q);
   return 0;
}