-5

I am new to C++ programming . Please, can someone explain to me how bool comparison operators work in user-defined data types?

#include <iostream>
#include <string>
#include <functional>

using namespace std;

class person {
public:
    float age;
    string name;

    bool operator<(const person& rhs) const
    {
        return age < rhs.age;
    }

    bool operator>(const person& rhs) const
    {
        return age > rhs.age;
    }
};
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 6
    They're explained in your book. No need to repeat its contents here. – Asteroids With Wings Jul 10 '20 at 16:31
  • 3
    By the way, those are not particularly good comparison operator implementations for that class. – Asteroids With Wings Jul 10 '20 at 16:31
  • 2
    What exactly don't you understand? What's wrong with what you've already implemented? – Alan Birtles Jul 10 '20 at 16:32
  • 1
    It's an operator that returns a `bool`. `true` if the relation holds, `false` if it doesn't. – Jesper Juhl Jul 10 '20 at 16:32
  • 2
    Look up tutorials on *operator overloading*. – HolyBlackCat Jul 10 '20 at 16:34
  • They don't necessarily have to be `bool` operators. For user-defined types, you can define the behaviour of the operator function and it can return any type. What part is it that's not clear to you? – Amal K Jul 10 '20 at 16:35
  • 1
    It would take far too long to explain all the details of operator overloading. If that's what you want then you should look at a C++ book. On the other hand if you have a *specific* question or doubt you should explain what that is. That kind of question can be easily answered. – john Jul 10 '20 at 16:39
  • Doesn't directly address the question, but worthwhile reading all the same: [What are the basic rules and idioms for operator overloading?](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading) – user4581301 Jul 10 '20 at 16:54
  • 2
    What do you not understand? (It is unclear what you are having trouble with in your question.) Are you learning C++ from a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)? – Eljay Jul 10 '20 at 16:57

1 Answers1

2

C++ standard provides a better way to use the operators directly with objects. For example:

class abc {
public:
    abc() {}
};

// declaration
abc a1, a2, a3;
a3 = a1 + a2; // impossible expression without operator overload

Here, you can't use arithmetic operator +, since they're objects of the type of abc class and the expression can't be evaluated in that way.

The concept the code had used is called operator overloading and it could be used in structs too.

So, there are two methods to achieve this:

  1. Define a class member function which compares the private variables members of the class.

  2. Or, use operator overloading to define what the operator(s), such as +, <<, etc. should do when they're used with the corresponding class objects (in that way you've used in the code).

Rohan Bari
  • 7,482
  • 3
  • 14
  • 34