Question
The existing code provides a class Person and sets up 5 generations of women. Maria is the daughter of Jan who is the daughter of
The existing code provides a class Person and sets up 5 generations of women. Maria is the daughter of Jan who is the daughter of Naomi who is the daughter of Nancy who is the daughter of Sue. Write code for the printGrandma function to print out the name of the grandmother of the Person p who is passed to the function.
#include
using namespace std;
class Person { private: Person* mother; //memory address of our spouse, nullptr = not married string name; public: Person(string nameValue, Person* motherPtr) { name = nameValue; mother = motherPtr; } Person* getMother() { return mother; }
string getName() { return name; } };
void printGrandma(Person& p) { //Do not modify anything on or above the line below this //YOUR_CODE_BELOW
//YOUR_CODE
//YOUR_CODE_ABOVE //Do not modify anything on or below the line above this }
int main() { Person p1("Sue", nullptr); Person p2("Nancy", &p1); Person p3("Naomi", &p2); Person p4("Jan", &p3); Person p5("Maria", &p4); printGrandma(p3); printGrandma(p4); printGrandma(p5); }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started