9.4 — Overloading the comparison operators

Overloading the comparison operators is simple once you’ve learned how to overload the arithmetic operators.

Because the comparison operators are all binary operators that do not modify their operands, we will make our overloaded comparison operators friend functions.

Here’s an example Point class from the previous lesson with an overloaded operator== and operator!=.

The code here should be straightforward. Because the result of operator!= is the opposite of operator==, we define operator!= in terms of operator==, which helps keep things simpler, more error free, and reduces the amount of code we have to write.

It doesn’t really make sense to overload operator> or operator< for the Point class. What does it mean for a 3d point to be greater or less than another point? Greater than or less than isn't a concept we normally apply to 3d points, so it's better not to include those operators in the Point class, because the results of the operators (whatever you define them to be) would not be intuitive. Here's a different example with an overloaded operator>, operator<, operator>=, and operator<=:

This is also pretty straightforward. Note that there is some redundancy here as well. operator> and operator<= are logical opposites, so one could be defined in terms of the other. operator< and operator>= are also logical opposites, and one could be defined in terms of the other. In this case, I chose not to do so because the function definitions are so simple, and the comparison operator in the function name line up nicely with the comparison operator in the return statement.

9.5 -- Overloading unary operators +, -, and !
9.3 -- Overloading the I/O operators

10 comments to 9.4 — Overloading the comparison operators

  • int main()
    using namespace std;
    Cents a(80);
    Cents b(100);
    if (a > b)
    cout << "A is GREATER THAN B";
    cout << "B is GREATER THAN A" << endl;
    cout << "===================================" << endl;

    Cents x(80);
    Cents y(78);
    if (x <= y)
    cout << "X is LESS THAN OR EQUAL TO Y";
    cout << "Y is LESS THAN OR EQUAL TO X" << endl;

    cout << "===================================" << endl;

    Cents ict1(80);
    Cents ict2(200);
    if (ict1 < ict2)
    cout << "ICT1 is LESS THAN ICT2" <<endl;
    cout << "ICT2 is LESS THAN ICT1" << endl;

    cout << "===================================" <= ict4)
    cout << "ICT3 is GREATER THAN OR EQUAL TO ICT4";
    cout << "ICT4 is GREATER THAN OR EQUAL TO ICT3";
    return 0;
    // working okay by ifycent2 de ict

  • Onix_GCI

    Wouldn't the functions in the first example always return wrong result due to small rounding errors in double comparison?

  • j.howard

    Great tutorial as usual. Just as an idea the < and > operators could be used to compare the magnitude of the vector from the origin to the point in the Point class.

  • mslade

    Shouldn't all of the arguments to the operator overload functions be const?

    • xgiggs

      Yes, you're right, I think. If they aren't const, then those operators can't be used on const variables even though there is no real reason they shouldn't, since they don't actually change the variable values.

  • Ameerah

    thanks Alex very mutch i love you totorile

    simple &easy

  • Andrian

    Could you make an example to show how you can use one of the overloaded operators?

    • Sure.

      Cents cNickle(5);
      Cents cDime(10);
      if (cDime > cNickle) // uses overloaded Cents::operator>
          cout << "A dime is greater than a nickle";
          cout << "A nickle is greater than a dime";
  • David

    operator> is defined twice; operator< is missing.

    [ Fixed! -Alex ]

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">