Hello again! In this video, we are going to look at the equality and inequality operators.
The equality operator takes two objects of the same type and returns a bool. We invoke it by putting a equals
equals b, and it will be called as operator equals equals with arguments a and b.
The return value should be true when the two objects are equal.
So, whatever that means for two objects of the class to be equal. And if they are not equal, then it returns
false. the inequality operator is the not equals operator, and the return value is obviously the other
way round.
If the two objects are equal, it returns false.
Otherwise, it returns true.
If a class defines one of these operators, it probably also needs to define the other one, which
is trivial.
You can just implement the inequality operated by calling the equality operator, and inverting the result.
So here we have the arguments of the inequality operator.
We compare them, and if they are equal, we return false.
And if they are different, we return true.
We are going to demonstrate this with a basic class for a student.
So the student has a name and an identity number, and perhaps some other data and member functions that
we are not really interested in for this example.
So what does this actually mean for two students to be equal?
Does it mean they have the same name?
Well, not necessarily.
You can have two different people who have the same name. But the university or the college, or whatever,
does make sure that each student's ID number is unique.
So that is going to be different for each students.
So if we compare the name, we may get some false results.
We may think that two different students with the same name are the same person.
If we compare the ID's, we will never get these false results.
So here is our student class with the name and the ID.
I have been a bit lazy and used the friend keyword for the operators, but I have actually written it
out as a separate header and source file.
So it is not all bad news :)
And there is also a print member the function for displaying the private data.
And then, in the source file, we define these functions.
We have the equality operator, the inequality operator and the print function.
And there is also a separate main() in which we create some student objects.
So we have two students with the same name, just to check how that works.
But with different ID's. And then a completely different student. Then we print out the details and
we print out the results of comparing them.
So let's see what happens.
So we have two John Smiths, but they have different ID's, so they are regarded as different people.
Obviously, John Smith and Jack Jones are going to be different.
So that is all right. If we go to the student.cpp and change this.
So we are comparing the names.
And then it thinks that the two John Smiths are the same person, which is not the right way to do it.
So sometimes you need to stop and think a bit about what it actually means, for two objects of your class
to be equal.
The obvious comparison might not always be the correct one.
Okay, so that is it for this video.
I will see you next time, but until then, keep coding!