WEBVTT

00:00.390 --> 00:07.950
Hello again! In this video, we are going to look at operators and overloading. C++ defines internal operators

00:07.950 --> 00:09.060
for built-in types.

00:10.230 --> 00:16.740
We have the plus operator for adding two ints, for example. So we can say c equals a plus b.

00:17.370 --> 00:21.750
This will actually call a function called operator plus, which takes a and b as arguments.

00:22.380 --> 00:26.370
This returns an int, and then the return value is stored in s.

00:26.970 --> 00:30.050
So there is an internal function inside C++,

00:30.600 --> 00:33.270
operator plus, which takes two ints and returns another int.

00:34.920 --> 00:39.400
As another example, C++ has the equality operator for comparing two ints.

00:40.200 --> 00:45.900
If we have a equals equals b. Built-in types are not really objects, but you can think of this

00:46.200 --> 00:48.060
as if it is a call to a member function.

00:48.840 --> 00:52.680
So the equality operator is called on a, with the argument b.

00:54.510 --> 00:59.400
And there are examples in the Standard Library as well. For example, with the string. The plus operator

00:59.400 --> 01:06.630
will add two strings and it is exactly the same except for the type, so it takes two strings and returns

01:06.640 --> 01:07.260
another string.

01:08.800 --> 01:15.100
The equality operator, that really is a member function this time, so it is called on the first string

01:15.520 --> 01:17.560
and it takes the second string as argument.

01:17.920 --> 01:24.670
So really, you can see that there are two types of operators; ones which take two arguments and are

01:25.030 --> 01:31.090
called as non-member functions, and ones which take a single argument and are called as member functions.

01:34.590 --> 01:40.110
And there are actually special names for this, or special terms. An operator which takes a single argument

01:40.110 --> 01:47.850
is unary and an operator which takes two arguments is binary. And there is actually one example of

01:48.060 --> 01:51.690
an operator which can be both, or it has two different versions. One for each.

01:52.770 --> 01:54.200
There is an operator minus.

01:54.210 --> 01:58.830
So that does the obvious thing of subtracting two numbers, which is a binary operation.

01:59.730 --> 02:01.320
It takes two arguments, a and b.

02:01.710 --> 02:06.240
But there is also a unary operator, which just changes the sign of the argument.

02:06.690 --> 02:11.190
So that will take b, and it will return b, but with the opposite sign.

02:13.130 --> 02:17.180
In fact, C++ has a ternary operator, which takes three arguments.

02:18.750 --> 02:27.840
This is a shorthand way of watching an if statement. So "a query b colon c" is equivalent to "if a b, else c" and

02:27.840 --> 02:30.570
that is implemented as an operator, which has three arguments.

02:33.980 --> 02:40.650
So, ignoring the ternary operator for the moment, opeartors take either one or two arguments.

02:40.670 --> 02:43.550
Operators can be member functions or non-member functions.

02:43.940 --> 02:50.420
Normally, if you have a unary operator, it is implemented as a mamber function on an object. And binary operators

02:50.420 --> 02:54.530
are usually non-member functions, so they take both objects as arguments.

02:57.090 --> 03:03.270
The classes which are defined in the Standard Library, usually have operators which will act on objects

03:03.270 --> 03:03.930
of that class.

03:04.470 --> 03:10.080
So as we saw with the string, with the plus operator and the equality operator, these have the same

03:10.080 --> 03:15.270
syntax as operators for the built-in types, and they have similar behavior.

03:15.660 --> 03:17.790
So the plus sign will add two strings.

03:18.330 --> 03:23.550
It is not going to reverse the string or remove alternate elements. That would be very confusing!

03:26.640 --> 03:33.840
And C++ allows us to define our own operators. If we write a class in C++, it does not have any operators,

03:34.500 --> 03:40.710
except for the assignment operator, if the compiler synthesizes that. But we can define other operators

03:40.710 --> 03:43.470
if we want to, which will act on objects of our class.

03:44.040 --> 03:48.670
And this is known as "overloading" the operators. "Overloading" here

03:48.680 --> 03:51.630
means giving a different meaning to an existing symbol.

03:52.020 --> 03:57.120
So if we have overloaded functions, then we have different meanings for different arguments. And we

03:57.120 --> 03:58.320
have overloaded operators.

03:58.320 --> 04:01.560
We have different meanings for different types that they operate upon.

04:03.660 --> 04:09.180
We can only use the symbols which are supplied for built-in operators, and we have to use the same

04:09.180 --> 04:13.910
syntax. So we are not allowed to invent our own operators or our own syntax.

04:13.920 --> 04:14.550
Thank goodness!

04:15.900 --> 04:19.150
Life is confusing enough in C++ as it is, without this!

04:20.460 --> 04:24.960
And when we write our operators, they should have similar semantics to the built-in ones.

04:25.290 --> 04:30.120
So that is just to avoid confusing people who are going to use your class and these operators.

04:30.510 --> 04:32.370
Okay, so that is it for this video.

04:32.760 --> 04:33.680
I will see you next time.

04:33.690 --> 04:35.730
But until then, keep coding!
