WEBVTT

00:00.870 --> 00:06.750
Hello again! In this video, we are going to look at addition operators. We briefly looked at the plus

00:06.750 --> 00:09.720
operator before, and now we are going to go into a bit more detail.

00:12.240 --> 00:18.600
So this is the prototype of the plus operator. It takes two objects of the same type, preferably

00:18.600 --> 00:24.670
by reference to const, because that is the most efficient way to pass objects, and it returns and another object

00:24.720 --> 00:28.770
of the same type by value. To invoke it, we just put "a plus b".

00:29.430 --> 00:36.510
And that will be called as operator plus, with arguments a and then b. The return value is, or should be,

00:36.510 --> 00:38.130
the sum of the two objects.

00:38.610 --> 00:40.020
And that will be returned by value.

00:40.530 --> 00:42.690
And this is defined as a non-member function.

00:43.080 --> 00:45.150
And you might like to think about why that is.

00:47.460 --> 00:51.720
The reason is that we have a binary operator. We may want to be able to perform a conversion.

00:52.080 --> 00:58.530
If a or b is not type T, but some other type that can be converted to type T, then we want that

00:58.530 --> 01:04.620
to work very naturally. As we did with the string class, with the C-style string.

01:04.800 --> 01:07.920
And for that to work, it needs to be a non-member function.

01:11.540 --> 01:13.500
There is also a plus equals operator.

01:13.520 --> 01:19.070
So this will add its argument to the object, and then return the modified object.

01:20.740 --> 01:26.380
This takes one argument of the appropriate type, and returns the modified object by reference. The

01:26.380 --> 01:32.560
compiler will call this as operator plus equals on the object a, with argument b, and that will return

01:32.560 --> 01:34.240
the modified version of a.

01:34.900 --> 01:37.060
And that is defined as a member function.

01:40.140 --> 01:44.010
And the reason for that is that we should write operators as member functions whenever we can.

01:44.430 --> 01:47.190
Especially if, like this one, they modify the object.

01:50.280 --> 01:52.020
So how do we implement these operators?

01:52.380 --> 01:57.870
The obvious thing is to write a member function for plus equals, and a separate function outside the

01:57.870 --> 01:59.280
class for the plus operator.

02:00.030 --> 02:00.840
And you can do that.

02:01.270 --> 02:05.400
But one of the key rules is "DRY", d-r-y. Do not Repeat Yourself!

02:06.030 --> 02:09.720
These will basically do the same thing, so there is going to be duplicated code.

02:11.820 --> 02:17.010
And we can actually avoid that. We can implement the plus operator in terms of the plus equals operator.

02:18.850 --> 02:24.820
The way to do that is in our plus operator. So this is going to be outside the class, then we make

02:24.820 --> 02:26.850
a copy of the first argument.

02:28.510 --> 02:34.980
The one on the left. Then we call the plus equals operator on this copy.

02:34.990 --> 02:39.790
Then we pass the second argument as the argument to the plus equals call.

02:40.780 --> 02:43.030
And then we return this modified copy.

02:43.840 --> 02:49.930
So the original argument is still unmodified. And that way, we only need to write the code once.

02:51.250 --> 02:54.730
So this is an example of code re-use, which is always a good thing to do.

02:55.450 --> 02:58.840
We only need to write the addition code once, in the plus equals member function.

02:59.500 --> 03:04.090
And if we change our mind. If we add more data members or we decide that we want to do the addition

03:04.090 --> 03:09.100
differently, then we only need to alter the plus equals member function. And we do not need to touch

03:09.100 --> 03:10.540
the plus operator at all.

03:13.480 --> 03:20.380
And now I m going to follow the well-trodden path of C++ instructors and implement a simple complex number class.

03:21.190 --> 03:23.980
There is actually a complex number class now in the library.

03:24.730 --> 03:28.360
I am going to call this one "Complex", with a capital 'C', so there is no confusion.

03:29.170 --> 03:31.210
And this is going to be a very simple implementation.

03:33.010 --> 03:34.450
Complex numbers have two parts.

03:34.720 --> 03:40.030
So we have the real part, which "ordinary" numbers have. And then we have this imaginary part, which is,

03:40.030 --> 03:45.220
if you like, a kind of extra dimension to the complex number. I am going to explain complex numbers in

03:45.220 --> 03:48.040
detail when we look at the library complex number.

03:48.050 --> 03:52.720
So if you do not understand complex numbers do not worry. You do not really need to know anything about them

03:52.720 --> 03:53.380
for this video.

03:56.200 --> 04:00.910
And then we have a constructor, which will create a complex object from two doubles.

04:01.780 --> 04:06.730
So this is just going to override the default initial values of 0.0 for the members.

04:09.010 --> 04:13.120
So we said we are going to define the plus equals operator as a memory function.

04:14.110 --> 04:20.050
This needs to add the real and imaginary parts of the object separately, and then return the modified

04:20.050 --> 04:20.500
object.

04:22.210 --> 04:23.590
So it would look something like this.

04:24.580 --> 04:30.730
We have our plus equals operator, which takes a Complex object by reference to const, and returns a Complex

04:30.730 --> 04:31.960
object by reference.

04:32.950 --> 04:38.230
Then we add the real part of the argument to our real part, and the imaginary part of the argument

04:38.590 --> 04:40.120
to our imaginary part.

04:40.900 --> 04:43.960
And then we return the modified object by reference.

04:45.190 --> 04:50.290
So that is the actual details of the addition. And then, for the plus operator.

04:50.680 --> 04:52.900
So this is going to be defined outside the class.

04:55.580 --> 04:58.850
We take two Complex objects by reference to const.

04:59.990 --> 05:02.180
We return a Complex object by value.

05:03.230 --> 05:06.220
Then we make our copy of the left hand argument.

05:07.580 --> 05:11.090
Then we call the plus equals operator on that argument.

05:11.390 --> 05:17.090
So this is going to addd the members of the right hand argument, to the members of a copy of the left

05:17.090 --> 05:19.820
hand side. And then we return that by value.

05:20.120 --> 05:23.270
So that is going to be the modified version of the left hand argument.

05:23.780 --> 05:25.310
The original is still unchanged,

05:25.370 --> 05:25.880
of course.

05:27.630 --> 05:28.890
So let's implement this.

05:29.190 --> 05:34.470
So here is our Complex class, with the real and imaginary members and our constructor to initialize

05:34.470 --> 05:34.680
them.

05:36.420 --> 05:42.850
Here is our plus equals member function, just like it was on the slide. And I have also added a print

05:42.870 --> 05:45.960
function to display the values of the members.

05:48.230 --> 05:53.600
And then outside the class, we have the operator plus. Again, the same as on the slide.

05:54.980 --> 06:00.320
So we are just delegating to the plus equals function. So that can access the private data of the

06:00.320 --> 06:00.680
class.

06:03.900 --> 06:09.180
In the main function, we create some objects. And we try out our member functions and operators.

06:10.350 --> 06:11.370
So let's see what we get.

06:15.970 --> 06:21.440
So we have two objects with real part 1 and imaginary part 2. Real part 3 and imaginary part 4.

06:22.300 --> 06:27.340
If we add them together, we get 4 for the real part and 6 for the imaginary.

06:28.360 --> 06:34.570
And then with the plus equals operator, after doing that, c1 has the real part 4 and the imaginary

06:34.570 --> 06:35.140
part 6.

06:35.980 --> 06:37.210
So that all works correctly.

06:38.020 --> 06:40.720
So what happens if we want a type conversion?

06:42.040 --> 06:44.110
So let's add some code to do that.

06:48.430 --> 06:50.140
Let's create a new object.

06:53.970 --> 06:57.750
And then, let's say, we want to have 1 plus c2.

06:59.280 --> 07:02.070
So 1 is actually a special case of a complex number.

07:02.670 --> 07:05.760
It's a complex number where the imaginary part is 0.

07:06.420 --> 07:08.070
So we should be able to convert this.

07:08.790 --> 07:12.120
But before we can do that, we need a constructor which takes a single argument.

07:12.780 --> 07:13.890
So let's add that...

07:22.590 --> 07:29.310
So this will just set the imaginary part to 0.0, because it's a real number and not a complex number.

07:30.180 --> 07:34.980
In fact, we do not need that, because the imaginary part has a default value of 0.

07:35.430 --> 07:36.690
So we can just remove that code.

07:39.270 --> 07:44.280
So again, another good use for in-place initializers, making the code a bit simpler.

07:47.260 --> 07:54.700
So we have this constructor now, which can convert a double, or an int coerced to double, into a complex

07:54.700 --> 07:55.030
number.

07:56.320 --> 08:04.450
And then this will call the non-member plus operator, and the compiler will convert the 1 into a Complex

08:04.450 --> 08:04.900
object.

08:05.910 --> 08:07.000
We hope so, anyway!

08:10.780 --> 08:11.290
And there we are.

08:12.010 --> 08:15.650
So our object c4, which is 1 plus c2.

08:15.670 --> 08:20.290
So it is, 3 plus 1 makes 4, and 4 plus 0 makes 4.

08:21.220 --> 08:21.480
Yep.

08:21.490 --> 08:22.450
So that all works.

08:24.220 --> 08:24.580
Okay.

08:24.580 --> 08:26.140
So that's it for this video.

08:26.560 --> 08:27.730
I'll see you next time.

08:28.000 --> 08:29.710
Until then, keep coding!
