0

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

2
Contributors
1
Reply
2
Views
12 Years
Discussion Span
Last Post by Dogtree
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';
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.