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

*easy, only 2 questions!!!!! You are given the following segment of code. All th

ID: 3721366 • Letter: #

Question

*easy, only 2 questions!!!!!

You are given the following segment of code. All the following C++ questions are based on this code.

class Owner {
    public:                int           iPhone;
  string     sEmail;
Owner(int phone, string email) { // constructor
  iPhone = phone; sEmail = email;
}
};

class Pet {
  public:                string sName;
Owner *pOwner;
Pet(string name, int phone, string email) { // constructor
  sName = name; pOwner = new Owner(phone, email);  
}
};

class Cat : public Pet     {    

public: string cBreed;

                                int iAge;

                                Cat(string name, int phone, string email, string breed, int age) . . . // write the constructor

};

class PetNode {
   public:           PetNode    *pNext;   
  Pet               *pNode;
  PetNode() { pNode = NULL; pNext = NULL; };
} *head = NULL; // head is the global pointer to the PetNode linked list

void insertPet();
void insertCat();
//...

void main(void) {
Pet* p;
Cat* c;
  PetNode* pn;
// input the data here
  insertPet(…);
  insertCat(…);
// ...
}

The following diagrams show the relationships among the classes and an example of the linked list constructed using the classes.

All the following questions are based on the given code above.

(1) Give the C++ code of the Cat class’s constructor. Note Cat inherits Pet. [3 points]

Cat(string name, int phone, string email, string breed, int age)

(2) Do you have to (without memory leak) write a destructor for the Cat class? If your answer is "yes", write the destructor for the class. If your answer is "no", explain your answer. [2 points]

The following diagrams show the relationships among the classes and an example of the linked list constructed using the classes PetNode pNext pNode PetNode Pet Node head pNext NULL Owner iPhone sEmail Pet pNode - pNode- containment sName Pet Owner Pet cName pOwner cName inheritance sName pOwnerF sBreed Owner at powner Breed Age IPhone · iPhone | cEmail Phone iAge cEmail cEmail

Explanation / Answer

1. Constructor for class Cat:


//call constructor of base class and then initialize the remaining variables

Cat(string name, int phone, string email, string breed, int age): Pet(name,phone,email) {
    cBreed = breed;
    iAge = age;
    }

2. The compiler creates a default destructor when we don't create a destructor of our own. The default destructor works fine unless we have dynamically allocated memory or pointer in class. When a class contains a pointer to memory allocated in class, we should write a destructor to release memory before the class instance is destroyed. This must be done to avoid memory leak.

The destructor is :

// calling destructor to delete pointer to Owner class

~Cat(){
        delete pOwner;
    }