943,969 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3210
  • C++ RSS
May 17th, 2005
0

overload functions

Expand Post »
can someone help me with this, or just help get me started.

write the definitions of the functions to overload the increment, decrement, arithmetic, and relational operators as a member of the class rectangeType.

write a test program that tests various operations on the class rectangleType
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
madt is offline Offline
16 posts
since May 2005
May 17th, 2005
0

Re: overload functions

This might give you a start for how to define them:
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <utility>
  3.  
  4. class Integer {
  5. int _val;
  6. public:
  7. Integer(): _val(0) {}
  8. Integer(int val): _val(val) {}
  9. int get() const { return _val; }
  10.  
  11. Integer operator+=(const Integer& rhs) { return _val += rhs._val; }
  12. Integer operator-=(const Integer& rhs) { return _val -= rhs._val; }
  13. Integer operator*=(const Integer& rhs) { return _val *= rhs._val; }
  14. Integer operator/=(const Integer& rhs) { return _val /= rhs._val; }
  15. Integer operator%=(const Integer& rhs) { return _val %= rhs._val; }
  16.  
  17. Integer operator+(const Integer& rhs) { return _val + rhs._val; }
  18. Integer operator-(const Integer& rhs) { return _val - rhs._val; }
  19. Integer operator*(const Integer& rhs) { return _val * rhs._val; }
  20. Integer operator/(const Integer& rhs) { return _val / rhs._val; }
  21. Integer operator%(const Integer& rhs) { return _val % rhs._val; }
  22.  
  23. Integer operator++() { return *this += 1; }
  24. Integer operator++(int) { Integer temp(*this); ++*this; return temp; }
  25. Integer operator--() { return *this -= 1; }
  26. Integer operator--(int) { Integer temp(*this); --*this; return temp; }
  27.  
  28. friend bool operator==(const Integer& lhs, const Integer& rhs)
  29. { return lhs._val == rhs._val; }
  30. friend bool operator<(const Integer& lhs, const Integer& rhs)
  31. { return lhs._val < rhs._val; }
  32. };
  33.  
  34. int main()
  35. {
  36. using namespace std::rel_ops; // Sneaky sneaky ;-)
  37.  
  38. Integer a(20);
  39.  
  40. // Op-assign operators
  41. std::cout << a.get() <<'\n';
  42. a += 2;
  43. std::cout << a.get() <<'\n';
  44. a -= 2;
  45. std::cout << a.get() <<'\n';
  46. a *= 2;
  47. std::cout << a.get() <<'\n';
  48. a /= 2;
  49. std::cout << a.get() <<'\n';
  50. a %= 2;
  51. std::cout << a.get() <<'\n';
  52.  
  53. a = 20;
  54. Integer c;
  55.  
  56. // Arithmetic operators
  57. std::cout << c.get() <<'\n';
  58. c = a + 2;
  59. std::cout << c.get() <<'\n';
  60. c = a - 2;
  61. std::cout << c.get() <<'\n';
  62. c = a * 2;
  63. std::cout << c.get() <<'\n';
  64. c = a / 2;
  65. std::cout << c.get() <<'\n';
  66. c = a % 2;
  67. std::cout << c.get() <<'\n';
  68.  
  69. a = 20;
  70.  
  71. // Increment/decrement
  72. std::cout << (++a).get() <<'\n';
  73. std::cout << (a++).get() <<'\n';
  74. std::cout << (--a).get() <<'\n';
  75. std::cout << (a--).get() <<'\n';
  76.  
  77. a = 20;
  78. c = 20;
  79.  
  80. // Relational operators
  81. std::cout << (a == c) << '\n';
  82. std::cout << (a != c) << '\n';
  83. std::cout << (a < c) << '\n';
  84. std::cout << (a <= c) << '\n';
  85. std::cout << (a > c) << '\n';
  86. std::cout << (a >= c) << '\n';
  87. }
Reputation Points: 35
Solved Threads: 3
Posting Whiz in Training
Dogtree is offline Offline
232 posts
since May 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Printing
Next Thread in C++ Forum Timeline: Outputting data





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC