fork download
  1. // Attached: HW_9f-1
  2. // ===========================================================
  3. // File: HW_9f-1
  4. // ===========================================================
  5. // Programmer: Elaine Torrez
  6. // Class: CMPR 121
  7. // ===========================================================
  8.  
  9. #include <iostream>
  10. #include <string>
  11. using namespace std;
  12.  
  13. class Dog
  14. {
  15. private:
  16. string name;
  17. float weight;
  18. int age;
  19.  
  20. public:
  21. Dog(string dogName, float dogWeight, int dogAge)
  22. {
  23. name = dogName;
  24. weight = dogWeight;
  25. age = dogAge;
  26. }
  27.  
  28. ~Dog()
  29. {
  30. }
  31.  
  32. void displayDog()
  33. {
  34. cout << "NAME: " << name << endl;
  35. cout << "WEIGHT: " << weight << " pounds" << endl;
  36. cout << "AGE: " << age << " years old." << endl;
  37. }
  38.  
  39. bool operator >= (int years)
  40. {
  41. return age >= years;
  42. }
  43.  
  44. bool operator < (Dog rightSide)
  45. {
  46. return weight < rightSide.weight;
  47. }
  48.  
  49. bool operator == (Dog rightSide)
  50. {
  51. return name == rightSide.name;
  52. }
  53.  
  54. friend ostream& operator << (ostream& out, Dog dogObject)
  55. {
  56. out << "NAME: " << dogObject.name << endl;
  57. out << "WEIGHT: " << dogObject.weight << " pounds" << endl;
  58. out << "AGE: " << dogObject.age << " years old.";
  59. return out;
  60. }
  61. };
  62.  
  63. int main()
  64. {
  65. Dog myDog("Spot", 5.5, 3);
  66. Dog yourDog("Jack", 4.5, 3);
  67.  
  68. if (myDog >= 2)
  69. cout << "The dog is at least 2 years old.\n\n";
  70. else
  71. cout << "The dog is less than 2 years old.\n\n";
  72.  
  73. if (yourDog < myDog)
  74. cout << "Your dog weighs less than my dog.\n\n";
  75. else
  76. cout << "Your dog does not weigh less than my dog.\n\n";
  77.  
  78. if (myDog == yourDog)
  79. cout << "They have the same name.\n\n";
  80. else
  81. cout << "They do not have the same name.\n\n";
  82.  
  83. cout << yourDog << endl;
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
The dog is at least 2 years old.

Your dog weighs less than my dog.

They do not have the same name.

NAME:   Jack
WEIGHT: 4.5 pounds
AGE:    3 years old.