fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class Person {
  5. private:
  6. int age;
  7.  
  8. public:
  9. Person(int a) {
  10. age = a;
  11. }
  12.  
  13. bool operator>(Person obj) {
  14. return age > obj.age;
  15. }
  16.  
  17. bool operator==(Person obj) {
  18. return age == obj.age;
  19. }
  20. };
  21.  
  22. int main() {
  23. int age1, age2;
  24.  
  25. cout << "Input 1: ";
  26. if (!(cin >> age1)) {
  27. cout << "Invalid" << endl;
  28. return 0;
  29. }
  30.  
  31. cout << "Input 2: ";
  32. if (!(cin >> age2)) {
  33. cout << "Invalid" << endl;
  34. return 0;
  35. }
  36.  
  37. if (age1 < 0 || age2 < 0) {
  38. cout << "Invalid" << endl;
  39. return 0;
  40. }
  41.  
  42. Person p1(age1);
  43. Person p2(age2);
  44.  
  45. if (p1 > p2) {
  46. cout << "Person 1 is larger" << endl;
  47. } else if (p2 > p1) {
  48. cout << "Person 2 is larger" << endl;
  49. } else if (p1 == p2) {
  50. cout << "Both are equal" << endl;
  51. }
  52.  
  53. return 0;
  54. }
  55.  
Success #stdin #stdout 0.01s 5296KB
stdin
Standard input is empty
stdout
Input 1: Invalid