fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class Movie {
  6. public:
  7. Movie(string movieTitle);
  8. void SetVotes(int numUpVotes, int numDownVotes) {
  9. upVotes = numUpVotes;
  10. downVotes = numDownVotes;
  11. }
  12. string GetTitle() const { return title; }
  13. int GetVoteDifference() const { return upVotes - downVotes; }
  14.  
  15. private:
  16. string title;
  17. int upVotes;
  18. int downVotes;
  19. };
  20.  
  21. Movie::Movie(string movieTitle) {
  22. title = movieTitle;
  23. upVotes = 0;
  24. downVotes = 0;
  25. }
  26.  
  27. bool operator==(const Movie& movie1, const Movie& movie2) {
  28. return movie1.GetVoteDifference() == movie2.GetVoteDifference();
  29. }
  30.  
  31. bool operator!=(const Movie& movie1, const Movie& movie2) {
  32. return !(movie1 == movie2);
  33. }
  34.  
  35. bool operator<(const Movie& movie1, const Movie& movie2) {
  36. return movie1.GetVoteDifference() < movie2.GetVoteDifference();
  37. }
  38.  
  39. bool operator>(const Movie& movie1, const Movie& movie2) {
  40. return movie2 < movie1;
  41. }
  42.  
  43. bool operator<=(const Movie& movie1, const Movie& movie2) {
  44. return !(movie1 > movie2);
  45. }
  46.  
  47. bool operator>=(const Movie& movie1, const Movie& movie2) {
  48. return !(movie1 < movie2);
  49. }
  50.  
  51. int main() {
  52. Movie movie1("Cars");
  53. Movie movie2("Up");
  54. Movie movie3("It");
  55. Movie movie4("Frozen");
  56.  
  57. movie1.SetVotes(11, 6);
  58. movie2.SetVotes(13, 5);
  59. movie3.SetVotes(14, 4);
  60. movie4.SetVotes(14, 2);
  61.  
  62. if (movie1 == movie3) {
  63. cout << movie1.GetTitle();
  64. cout << " is equal to ";
  65. cout << movie3.GetTitle() << endl;
  66. }
  67.  
  68. if (movie1 >= movie2) {
  69. cout << movie1.GetTitle();
  70. cout << " is at least as good as ";
  71. cout << movie2.GetTitle() << endl;
  72. }
  73. else if (movie1 < movie2) {
  74. cout << movie1.GetTitle();
  75. cout << " is worse than ";
  76. cout << movie2.GetTitle() << endl;
  77. }
  78.  
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Cars is worse than Up