954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

overload functions

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

madt
Newbie Poster
16 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This might give you a start for how to define them:

#include <iostream>
#include <utility>

class Integer {
  int _val;
public:
  Integer(): _val(0) {}
  Integer(int val): _val(val) {}
  int get() const { return _val; }

  Integer operator+=(const Integer& rhs) { return _val += rhs._val; }
  Integer operator-=(const Integer& rhs) { return _val -= rhs._val; }
  Integer operator*=(const Integer& rhs) { return _val *= rhs._val; }
  Integer operator/=(const Integer& rhs) { return _val /= rhs._val; }
  Integer operator%=(const Integer& rhs) { return _val %= rhs._val; }

  Integer operator+(const Integer& rhs) { return _val + rhs._val; }
  Integer operator-(const Integer& rhs) { return _val - rhs._val; }
  Integer operator*(const Integer& rhs) { return _val * rhs._val; }
  Integer operator/(const Integer& rhs) { return _val / rhs._val; }
  Integer operator%(const Integer& rhs) { return _val % rhs._val; }

  Integer operator++() { return *this += 1; }
  Integer operator++(int) { Integer temp(*this); ++*this; return temp; }
  Integer operator--() { return *this -= 1; }
  Integer operator--(int) { Integer temp(*this); --*this; return temp; }

  friend bool operator==(const Integer& lhs, const Integer& rhs)
  { return lhs._val == rhs._val; }
  friend bool operator<(const Integer& lhs, const Integer& rhs)
  { return lhs._val < rhs._val; }
};

int main()
{
  using namespace std::rel_ops; // Sneaky sneaky ;-)

  Integer a(20);

  // Op-assign operators
  std::cout << a.get() <<'\n';
  a += 2;
  std::cout << a.get() <<'\n';
  a -= 2;
  std::cout << a.get() <<'\n';
  a *= 2;
  std::cout << a.get() <<'\n';
  a /= 2;
  std::cout << a.get() <<'\n';
  a %= 2;
  std::cout << a.get() <<'\n';

  a = 20;
  Integer c;

  // Arithmetic operators
  std::cout << c.get() <<'\n';
  c = a + 2;
  std::cout << c.get() <<'\n';
  c = a - 2;
  std::cout << c.get() <<'\n';
  c = a * 2;
  std::cout << c.get() <<'\n';
  c = a / 2;
  std::cout << c.get() <<'\n';
  c = a % 2;
  std::cout << c.get() <<'\n';

  a = 20;

  // Increment/decrement
  std::cout << (++a).get() <<'\n';
  std::cout << (a++).get() <<'\n';
  std::cout << (--a).get() <<'\n';
  std::cout << (a--).get() <<'\n';

  a = 20;
  c = 20;

  // Relational operators
  std::cout << (a == c) << '\n';
  std::cout << (a != c) << '\n';
  std::cout << (a < c) << '\n';
  std::cout << (a <= c) << '\n';
  std::cout << (a > c) << '\n';
  std::cout << (a >= c) << '\n';
}
Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You