overload functions

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2005
Posts: 16
Reputation: madt is an unknown quantity at this point 
Solved Threads: 0
madt madt is offline Offline
Newbie Poster

overload functions

 
0
  #1
May 17th, 2005
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
Reply With Quote Quick reply to this message  
Join Date: May 2005
Posts: 232
Reputation: Dogtree is an unknown quantity at this point 
Solved Threads: 3
Dogtree's Avatar
Dogtree Dogtree is offline Offline
Posting Whiz in Training

Re: overload functions

 
0
  #2
May 17th, 2005
This might give you a start for how to define them:
  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC