WEBVTT

00:00:00.640 --> 00:00:04.070
Now that we know how to use pointers to point to objects,

00:00:04.080 --> 00:00:07.500
the rest of the module will be about using pointers inside

00:00:07.500 --> 00:00:09.760
of objects as data members.

00:00:10.140 --> 00:00:13.330
We'll begin this journey by imagining that we are genetic

00:00:13.330 --> 00:00:16.700
engineers. And just a disclaimer, I'm not very good at

00:00:16.700 --> 00:00:19.010
biology and you don't have to be either.

00:00:19.020 --> 00:00:20.760
This is just hypothetical.

00:00:21.140 --> 00:00:24.350
I created a simple DNA class, which holds the genetic

00:00:24.350 --> 00:00:27.460
code inside of this really large array.

00:00:27.840 --> 00:00:31.450
I used digit separator syntax to make this more readable.

00:00:31.460 --> 00:00:35.240
But the code array holds 60 million characters,

00:00:35.250 --> 00:00:38.360
which is equal to 60 MB of memory.

00:00:38.740 --> 00:00:42.260
The real DNA sequence would be much larger, but the reason for

00:00:42.260 --> 00:00:45.820
creating this big array is to make it impossible for us to use

00:00:45.820 --> 00:00:47.950
this DNA object on the stack.

00:00:48.340 --> 00:00:52.450
I will try to declare one just to prove that this is not possible.

00:00:53.440 --> 00:00:55.180
If I try to compile this program,

00:00:55.190 --> 00:00:59.670
I will get a segmentation fault because this is too big for stack memory.

00:00:59.680 --> 00:01:03.660
So let's instead allocate this DNA object on the heap.

00:01:04.239 --> 00:01:08.070
Instead of reading the genetic code from a file and making things more

00:01:08.070 --> 00:01:11.540
complicated, let's agree that the first four elements of the code array

00:01:11.540 --> 00:01:16.750
represent the whole sequence. And I want to initialize these four

00:01:16.750 --> 00:01:19.250
elements with a simple string literal.

00:01:20.240 --> 00:01:22.760
Of course, we need to define this constructor.

00:01:23.140 --> 00:01:27.090
We learned in one of the previous lessons that we can pass the string literal

00:01:27.090 --> 00:01:30.960
to a function by using the constant pointer to a char value.

00:01:31.340 --> 00:01:34.130
It has to be constant because the string literal is

00:01:34.140 --> 00:01:35.960
in a read only part of memory.

00:01:36.340 --> 00:01:40.570
I will simply rip out all of the characters from this literal and store

00:01:40.570 --> 00:01:43.960
them at the start of the code array in the same order.

00:01:44.340 --> 00:01:47.170
This is one of those situations where we actually use the

00:01:47.170 --> 00:01:50.310
array of characters as an array of characters.

00:01:50.320 --> 00:01:52.950
I don't want to use this code array as a string.

00:01:53.340 --> 00:01:57.370
It is literally just a sequence of characters and each one of these

00:01:57.370 --> 00:02:01.070
characters should be one of the four possible genetic bases,

00:02:01.080 --> 00:02:03.660
C, G, T, or A.

00:02:04.240 --> 00:02:08.150
I also want a way to print out this code, so I will create another

00:02:08.150 --> 00:02:11.600
function for that. We will simply loop over the first four

00:02:11.600 --> 00:02:14.310
characters and print them out to the terminal.

00:02:14.320 --> 00:02:17.090
We could have used the null character at the end of this

00:02:17.090 --> 00:02:20.370
sequence, but I still want to pretend that all of the

00:02:20.370 --> 00:02:22.260
other elements are initialized.

00:02:22.640 --> 00:02:25.250
And now this constructor should work.

00:02:25.260 --> 00:02:28.850
Let's test it by using the printGeneticCode function.

00:02:29.240 --> 00:02:33.350
Don't forget to use the arrow operator because sample is a pointer.

00:02:35.440 --> 00:02:38.300
The code is presented correctly, so it seems that

00:02:38.300 --> 00:02:40.360
we are done with the DNA class.

00:02:40.740 --> 00:02:42.880
Since we are now scientists,

00:02:42.890 --> 00:02:46.050
we also need to have subjects for the experiments. I

00:02:46.540 --> 00:02:49.150
will create a simple Subject class.

00:02:49.640 --> 00:02:54.070
Every subject has a unique integer ID and also a DNA sample

00:02:54.070 --> 00:02:56.560
which represents its genetic material.

00:02:56.940 --> 00:02:59.470
Since DNA class is really large,

00:02:59.480 --> 00:03:02.020
this kind of implementation would prevent us from

00:03:02.030 --> 00:03:04.860
instantiating subjects on the stack.

00:03:04.870 --> 00:03:08.820
So instead of having the sample inside of the subject itself,

00:03:08.830 --> 00:03:11.960
we can turn it into a pointer to a DNA object.

00:03:12.840 --> 00:03:17.130
Now, let's define the constructor. It will accept the ID of the

00:03:17.130 --> 00:03:21.060
subject and also the string literal for the DNA sample,

00:03:21.440 --> 00:03:26.120
I want to pass this string literal pointer to the DNA object, but the object

00:03:26.120 --> 00:03:29.890
doesn't exist yet, we only have an empty sample pointer.

00:03:29.900 --> 00:03:32.360
So inside of this constructor,

00:03:32.360 --> 00:03:37.220
we actually need to allocate space on the heap for this DNA object and

00:03:37.220 --> 00:03:41.560
store its location inside of the sample pointer. I will pass this

00:03:41.560 --> 00:03:46.360
pointer to a string literal down to the DNA constructor and this is

00:03:46.360 --> 00:03:49.680
where we need to start thinking about all of the side effects from

00:03:49.680 --> 00:03:52.160
using pointers as data members.

00:03:52.540 --> 00:03:56.250
Since we allocated this DNA resource inside of the constructor,

00:03:56.250 --> 00:03:59.370
we need to deallocate it inside of the destructor.

00:03:59.380 --> 00:04:05.520
So let's do this. I hope that you remember the concept of RAII

00:04:05.520 --> 00:04:08.550
from the first module because this is it.

00:04:09.340 --> 00:04:12.070
All of the resources that we allocate inside of the

00:04:12.070 --> 00:04:15.840
constructor need to be released inside of the destructor;

00:04:15.840 --> 00:04:18.550
otherwise, we are risking a memory leak.

00:04:18.940 --> 00:04:22.260
I also want to create a default constructor, which will just

00:04:22.270 --> 00:04:25.960
instantiate an empty subject with the ID of 0.

00:04:26.340 --> 00:04:30.180
The genetic placeholder code will also be a string of 0s

00:04:30.180 --> 00:04:33.030
because I want to prepare this place on the heap for

00:04:33.030 --> 00:04:35.280
potential future modifications,

00:04:35.400 --> 00:04:38.360
we will need this for some of the following lessons.

00:04:38.740 --> 00:04:39.500
Finally,

00:04:39.500 --> 00:04:43.780
let me also declare the printSubjectData function so that we can easily see

00:04:43.780 --> 00:04:46.860
all that we need to know about it on the terminal screen.

00:04:47.240 --> 00:04:52.140
I will first print out the subject's ID and then invoke the printGeneticCode

00:04:52.140 --> 00:04:55.160
function to print the genetic sequence from the sample.

00:04:56.140 --> 00:04:57.550
Now we can try this out.

00:04:58.340 --> 00:05:01.580
Let's remove all of this code from the main function and create a

00:05:01.580 --> 00:05:05.660
sheep subject with some random ID and genetic code.

00:05:06.340 --> 00:05:09.850
And let's also print the information about this subject.

00:05:10.740 --> 00:05:16.550
Okay, I'm going to compile this, and it seems that everything works as expected.

00:05:16.940 --> 00:05:19.480
Hopefully, I thought of a good enough example to

00:05:19.480 --> 00:05:22.550
make you see why RAII is important.

00:05:22.940 --> 00:05:26.750
This also applies to using pointers for any kind of resource,

00:05:26.760 --> 00:05:28.850
not just objects on the heap.

00:05:29.240 --> 00:05:32.720
Maybe you need to access a file through the pointer, and you open a

00:05:32.720 --> 00:05:36.860
connection to it in the constructor, but remember to always release

00:05:36.860 --> 00:05:39.160
resources inside of the destructor.

00:05:39.540 --> 00:05:41.790
Now that we defined this subject class,

00:05:41.790 --> 00:05:45.260
we can continue expanding it in the following lessons.

00:05:45.740 --> 00:05:47.550
And as you will see,

00:05:47.560 --> 00:05:50.380
managing resources with pointers from inside of the

00:05:50.380 --> 00:05:53.150
class is not as simple as it seems.
