Anton A. answered 05/28/20
Self-taught and Experienced Visual C++ Developer and Mentor/Tutor
Depending on your class design you may want to either overload operator==() or operator!=() or both. It truly depends what the class is supposed to do.
First of all, let's make sure we are clear on what object are. Objects are instances of a class. And a class is a blueprint for an object.
That said, if you have a class Person
How do you compare Person a or Person b?
If you would try to do the following:
How does the complier knows how to compare these two Person objects? Answer: it doesn't. The programmer has to code it out like so.
And yes, if you want to use either
Then one has to include both.
Now in most cases., either one or or the other comparison operator will suffice. In most cases, one can get away with just one (usually operator==()); but if one needs operator!=(), then one has to code it out. This goes for ALL of the comparison operators (like operator<() and operator>() ).