1. Binary operator #

Created Saturday 11 January 2020

Rationale:

Body is the same for both.

Notes:

In C++ we can overload most operators like +, -, [], -> etc. But we cannot overload all the operators. Some of the operators cannot be overloaded, viz:

  1. . Dot operator
  2. ? : Ternary operator
  3. :: Scope resolution operator
  4. .* Pointer to member operator
  5. sizeof object size operator
  6. typeid object type operator

DTSPST These operators cannot be overloaded because if we overload them it will make serious programming issues.

For an example the sizeof operator returns the size of the object or datatype as an operand. This is evaluated by the compiler. It cannot be evaluated during runtime. So we cannot overload it.

Some examples:

Fraction operator+(Fraction const &f2)
{
    Fraction ret(*this);
    int lcm = denominator * f2.denominator;
    int x = lcm / denominator;
    int y = lcm / f2.denominator;
    ret.numerator = x * numerator + (y * f2.numerator);
    ret.denominator = lcm;
    ret.simplify();
    return ret;
}
// Replace + by ==, -, *,/ . All these are binary functions.