Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Pet {
  5. string name;
  6. int age;
  7. public:
  8. Pet(string nm ="Kitty",int ag =1)
  9. { name = nm;
  10. age = ag;
  11. }
  12. string getName()
  13. { return name;
  14. }
  15. int getAge()
  16. { return age;
  17. }
  18.  
  19.  
  20. // Instructions: You are required to complete the overloading code operator function
  21.  
  22. Pet operator + (const Pet &); // Overloaded +
  23. Pet operator - (const Pet &); // Overloaded -
  24. Pet operator * (const Pet &); // Overloaded *
  25. bool operator > (const Pet &); // Overloaded >
  26. bool operator < (const Pet &); // Overloaded <
  27. bool operator == (constPet &); // Overloaded ==
  28.  
  29.  
  30. };
  31. // Main function for the program
  32. int main() {
  33. // declare instances of pets
  34. Pet pet1("Poppy",2),pet2("Boy",5), pet3;
  35.  
  36. pet3 = pet1 + pet2; // pet1.operator+(pet2)
  37.  
  38. cout << " Pet 1 name : " << pet1.getName() << endl;
  39. cout << " Pet 1 age : " << pet1.getAge() << endl;
  40. cout << " Pet 2 name : " << pet2.getName() << endl;
  41. cout << " Pet 2 age : " << pet2.getAge() << endl;
  42. cout << " Pet 3 name : " << pet3.getName() << endl;
  43. cout << " Pet 3 age : " << pet3.getAge() << endl;
  44.  
  45. if( pet1 < pet2 ) { //pet1.operator<(pet2);
  46. cout << pet1.getName() << " is younger than " << pet2.getName();
  47. if( pet1 == pet2)
  48. cout << "The pets have same page"<< endl;
  49. else
  50. cout << "Thay are not same age" << endl;
  51. // Provide suitable if statement for operator >
  52.  
  53.  
  54.  
  55.  
  56.  
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement