WEBVTT

00:00.180 --> 00:04.680
Hello again! In this video, we are going to look at static type and dynamic type.

00:06.530 --> 00:12.200
In the last video, we were looking at this "pShape", which is declared as pointed to a Shape.

00:12.620 --> 00:18.950
But the actual address is the address of a Circle object and Circle is a child class of Shape.

00:19.550 --> 00:22.190
So what actually is the type of pShape?

00:22.700 --> 00:25.850
Is it a pointer to Shape, or pointer to Circle?

00:27.950 --> 00:35.510
And the correct answer is both! In C++, variables actually have two types: the static type and the dynamic

00:35.510 --> 00:35.900
type.

00:36.590 --> 00:37.970
So let's have a quick look at this.

00:40.050 --> 00:46.200
The static type is the one that is used in the declaration of the variable, so that is what the compiler

00:46.200 --> 00:46.620
sees.

00:47.310 --> 00:54.000
So we have int, pointer to int, Circle, pointer to Circle, reference to Circle and so on.

00:57.240 --> 01:01.290
In the case of our pShape, this is declared as a pointer to a Shape.

01:01.710 --> 01:09.240
So the static type is pointer to Shape. And similarly for reference, the static type is reference

01:09.240 --> 01:09.990
to Shape.

01:11.270 --> 01:16.910
And similarly, for references, the static type is reference to Shape.

01:22.460 --> 01:28.010
The dynamic type depends on the variable that is actually in memory. And usually that is going to be the

01:28.010 --> 01:29.420
same as the declaration.

01:29.780 --> 01:33.230
So if we have an int, there is going to be an int object in memory.

01:33.740 --> 01:37.940
If we have a pointer to int, that's going to be pointing to an int object, and so on.

01:40.800 --> 01:47.310
However, if we have a pointer or reference to base class, it can be different. With our pShape,

01:47.310 --> 01:49.140
the actual object is a Circle.

01:49.590 --> 01:56.970
So the dynamic type of pShape is pointer to Circle. And for a reference, that would be reference

01:56.970 --> 01:57.630
to Circle.

02:02.310 --> 02:09.540
C++ is very, very keen on static typing! The compiler uses it for type checking and deciding which

02:09.570 --> 02:10.350
function to call.

02:11.190 --> 02:13.950
So this means there is less work to be done

02:14.220 --> 02:18.810
at run time, so the program runs faster, and also it makes it easier to optimize the code.

02:20.260 --> 02:26.350
The dynamic type is only used when we have a pointer or reference to a base class. In that case, the

02:26.350 --> 02:29.020
compiler does not decide which member function is called.

02:29.770 --> 02:34.960
Instead, the program will choose the function at run time, and it will choose the correct one for the object

02:35.020 --> 02:36.010
that is in memory.

02:40.430 --> 02:44.980
To do this, we need to declare the member function as virtual in the base class.

02:47.740 --> 02:52.750
So, in the Shape class, we put "virtual", before we declare the function.

02:53.710 --> 02:59.460
And then, when we call it through the pointer to Shape, the compiler will not get to decide that this

02:59.470 --> 03:01.060
is a member function of Shape.

03:01.720 --> 03:04.960
Instead, the program will decide that this is a Circle object.

03:05.410 --> 03:07.960
So it is going to call the Circle version of draw().

03:10.040 --> 03:12.110
So let's quickly try this out.

03:12.530 --> 03:16.090
This is almost the same program as we had before, with just one small change.

03:17.730 --> 03:21.360
So we have our vector of pointers to Shape objects.

03:22.290 --> 03:29.520
We push a pointer to Circle onto it, and then we iterate through the elements and we call the draw() member function.

03:30.870 --> 03:32.590
Before, we were using the static type.

03:32.610 --> 03:37.050
So the compiler always choose the Shape version of draw().

03:38.070 --> 03:41.370
We have now declared the draw() member function virtual.

03:42.060 --> 03:47.220
So the program should decide that we have a Circle object, and it is going to call the Circle version of

03:47.220 --> 03:47.580
draw().

03:49.770 --> 03:52.620
So there it is. We have actually called the Circle version.

03:53.340 --> 03:59.220
So the pointer to Shape has the static type of pointer to Shape, but the dynamic tight is pointer

03:59.220 --> 04:02.700
to a Circle. And that called the Circle member function.

04:03.930 --> 04:05.310
Okay, so that is it for this video.

04:05.670 --> 04:06.480
I will see you next time.

04:06.480 --> 04:08.480
But until then, keep coding!
