You are given the following segment of code. All the following C++ questions are
ID: 3721324 • Letter: Y
Question
You are given the following segment of code. All the following C++ questions are based on this code class Ownerf public int Phone, string sEmail; Owner(int phone, string email)f II constructor iPhone phone; sEmail email; class Pet public string sName; Owner "pOwner; Pet(string name, int phone, string email) f II 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)... I/ write the constructor class PetNode public PetNode pNext Pet PetNode() { pNode = NULL; pNext = NULL; }; pNode; )'head = NULL; // head is the global pointer to the PetNode linked list void insertPet); void insertCat) Il void main(void) Pet* p; Cat* c; PetNode* pn; // input the data here insertPet(...); insertCat(...);Explanation / Answer
1)
Pet *objpet; //this line is before the initialization of Cat constructor
Cat(string name,int phone,string email,string breed,int age)
{
cBreed=breed;
iAge=age;
objpet=new Pet(name,phone,email);
}
2)
A destructor has the same name as the class name, prefixed with a tilde (~) and it can neither return a value nor can it take any parameters
to create a destructor for the class Cat we just have to write
~Cat();
3)
void insertAtEnd(struct node* head, int num){
/* Input validation */
if (head == NULL) {
printf("Error : Invalid node pointer !!! ");
return;
}
/* creates a new node */
struct node* newNode =(struct node*) malloc(sizeof(struct node));
newNode->data = num;
newNode->next = NULL;
/* Traverse from head to last node */
while(head->next != NULL)
head = head->next;
/* Insert newNode after Tail node */
head->next = newNode;
}
Here I have shown that how can we add a node to the end of the linked list
If you have any query do comment below and if you like my solution please give a thumbs up!
Thanks :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.