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

You are given a segment of code in the previous question. The following question

ID: 3721369 • Letter: Y

Question

You are given a segment of code in the previous question. The following questions are based on the same code, where the Cat class is as follows.

class Cat : public Pet     {   
   public:                string cBreed;
int iAge;
Cat(string name, int phone, string email, string breed, int age) . . . // write the constructor
};

Answer the following questions:

(1) Define an overloading operator Cat>, which can compare two Cat objects to determine which cat is older. [3 points]

(2) Can the overloading operator Cat> be applied to two Pet objects? Explain your answer.    [2 point]

Explanation / Answer

Ques 1.

bool operator>(Cat& ob)

{

    // if the age of calling object is more

    if( this->iAge > ob.iAge )

        return true;

    // if the age of calling object is less

    else

        return false;

}

Ques 2.

We can't overload > operator with 2 operators as when we compare two objects, the object on the left is treated as the calling object and is referenced by this. So, we just have 1 argument.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote