WEBVTT

00:00.480 --> 00:05.190
Hello again! In this video, we are going to look at the equality and inequality operators.

00:07.120 --> 00:13.930
The equality operator takes two objects of the same type and returns a bool. We invoke it by putting a equals

00:13.930 --> 00:18.820
equals b, and it will be called as operator equals equals with arguments a and b.

00:19.750 --> 00:23.800
The return value should be true when the two objects are equal.

00:24.250 --> 00:30.490
So, whatever that means for two objects of the class to be equal. And if they are not equal, then it returns

00:30.490 --> 00:39.280
false. the inequality operator is the not equals operator, and the return value is obviously the other

00:39.280 --> 00:39.790
way round.

00:40.060 --> 00:42.790
If the two objects are equal, it returns false.

00:43.450 --> 00:44.950
Otherwise, it returns true.

00:49.510 --> 00:55.000
If a class defines one of these operators, it probably also needs to define the other one, which

00:55.000 --> 00:55.720
is trivial.

00:56.560 --> 01:02.650
You can just implement the inequality operated by calling the equality operator, and inverting the result.

01:03.280 --> 01:06.070
So here we have the arguments of the inequality operator.

01:06.610 --> 01:10.810
We compare them, and if they are equal, we return false.

01:10.810 --> 01:12.700
And if they are different, we return true.

01:14.700 --> 01:18.990
We are going to demonstrate this with a basic class for a student.

01:19.530 --> 01:24.960
So the student has a name and an identity number, and perhaps some other data and member functions that

01:24.960 --> 01:27.090
we are not really interested in for this example.

01:29.010 --> 01:31.890
So what does this actually mean for two students to be equal?

01:32.940 --> 01:34.200
Does it mean they have the same name?

01:34.440 --> 01:35.820
Well, not necessarily.

01:35.850 --> 01:41.670
You can have two different people who have the same name. But the university or the college, or whatever,

01:41.850 --> 01:44.670
does make sure that each student's ID number is unique.

01:45.090 --> 01:47.250
So that is going to be different for each students.

01:47.790 --> 01:51.720
So if we compare the name, we may get some false results.

01:51.720 --> 01:54.750
We may think that two different students with the same name are the same person.

01:55.260 --> 01:58.320
If we compare the ID's, we will never get these false results.

01:58.890 --> 02:01.920
So here is our student class with the name and the ID.

02:02.820 --> 02:07.890
I have been a bit lazy and used the friend keyword for the operators, but I have actually written it

02:07.890 --> 02:10.290
out as a separate header and source file.

02:10.290 --> 02:12.210
So it is not all bad news :)

02:13.830 --> 02:17.880
And there is also a print member the function for displaying the private data.

02:20.280 --> 02:23.640
And then, in the source file, we define these functions.

02:23.640 --> 02:28.350
We have the equality operator, the inequality operator and the print function.

02:30.150 --> 02:35.500
And there is also a separate main() in which we create some student objects.

02:35.520 --> 02:38.940
So we have two students with the same name, just to check how that works.

02:39.150 --> 02:46.050
But with different ID's. And then a completely different student. Then we print out the details and

02:46.050 --> 02:49.530
we print out the results of comparing them.

02:50.460 --> 02:51.560
So let's see what happens.

02:55.350 --> 03:01.170
So we have two John Smiths, but they have different ID's, so they are regarded as different people.

03:01.920 --> 03:04.470
Obviously, John Smith and Jack Jones are going to be different.

03:05.730 --> 03:09.810
So that is all right. If we go to the student.cpp and change this.

03:09.810 --> 03:11.700
So we are comparing the names.

03:17.850 --> 03:23.250
And then it thinks that the two John Smiths are the same person, which is not the right way to do it.

03:23.460 --> 03:29.430
So sometimes you need to stop and think a bit about what it actually means, for two objects of your class

03:29.430 --> 03:30.210
to be equal.

03:30.690 --> 03:32.730
The obvious comparison might not always be the correct one.

03:33.660 --> 03:35.580
Okay, so that is it for this video.

03:35.910 --> 03:38.880
I will see you next time, but until then, keep coding!
