WEBVTT

00:07.970 --> 00:15.230
In this section we'll be talking about new ways to represent our data by creating our own user defined

00:15.230 --> 00:16.620
types.

00:16.720 --> 00:20.150
We'll leave this off by talking about structures.

00:20.160 --> 00:27.090
Structures are a way of combining multiple different types into one bigger type.

00:27.110 --> 00:34.910
So for example there's no built in book type for C++ but we can make it ourselves and here's how you

00:34.910 --> 00:35.410
do that.

00:35.420 --> 00:40.190
So we say structure which is short for structure and key word.

00:40.560 --> 00:41.320
OK.

00:41.750 --> 00:52.450
And we say the name of our type is going to call a book type and noticed that we have the Brace's here

00:52.540 --> 00:53.400
statement.

00:53.690 --> 01:00.610
We're put a few more some statements inside of each statement block.

01:01.090 --> 01:11.220
And these statements will be the sort of definition of what our book type is composed of OK.

01:11.260 --> 01:17.410
So for example we could have a book ID which is it.

01:17.590 --> 01:24.810
It has a string which is the title of the book.

01:26.140 --> 01:29.590
This is the author

01:34.180 --> 01:40.390
and maybe the year it was published in Libya as well.

01:44.020 --> 01:52.090
So inside this block redeclare all the inner types that make up our structure.

01:52.170 --> 01:57.860
And so that's what that's what these five variables are.

01:58.050 --> 01:58.620
OK.

01:58.750 --> 02:03.510
So each of these variables inside belong to this structure.

02:03.510 --> 02:11.120
In other words and this is really just a way of grouping variables together into our own type.

02:12.360 --> 02:21.130
And remember that this is this is only the declaration of our type and defined by these five.

02:21.420 --> 02:24.320
This case five variables.

02:25.680 --> 02:30.010
But this isn't the this isn't a variable itself.

02:30.120 --> 02:34.810
This whole thing is not a variable it's the declaration type.

02:35.150 --> 02:39.760
So if you want to actually make a variable Here's how you do it.

02:39.770 --> 02:48.850
So this book type like this and we give it in the book say Okay.

02:49.260 --> 03:01.640
So just like we had with like an integer you say book type type and then book as very well me.

03:01.870 --> 03:02.690
OK.

03:03.010 --> 03:09.620
So you can actually access all these members right.

03:09.640 --> 03:16.800
Since we we know we have five member variables of this book.

03:17.020 --> 03:17.680
OK.

03:18.130 --> 03:23.580
So how you access them is through the dot operator.

03:23.620 --> 03:29.560
So you've actually seen it before with seat in but it's not exactly the same thing.

03:30.010 --> 03:31.210
In this case.

03:31.360 --> 03:39.710
So here you say a book in say want to access use e book ID.

03:39.720 --> 03:47.170
So you say a book Dot and will even notice in eclipse that it brings up kind of the completions of what

03:49.000 --> 03:51.760
the member variables are.

03:52.330 --> 03:54.270
The author of book ID.

03:54.700 --> 03:55.890
Oh year.

03:56.320 --> 04:03.460
So let's say one book I read press Enter him and set the book at the king's house.

04:05.670 --> 04:08.520
And we use a function for the

04:11.400 --> 04:24.330
title and author here called Sturr copy which will copy a string into our Auray book title.

04:24.950 --> 04:27.640
Know.

04:28.050 --> 04:28.600
It's

04:31.660 --> 04:40.500
so we're just putting this string literal into the title and copy all of it and even put the character

04:40.620 --> 04:44.850
for a while and then stir up copy

04:49.700 --> 04:50.380
offer

04:53.120 --> 04:59.260
me 100 points you know is Dickins

05:15.790 --> 05:20.930
year a million points you know.

05:21.050 --> 05:22.360
Ouch.

05:23.280 --> 05:30.800
You know nine in price say that's OK.

05:31.090 --> 05:39.790
So the reason the dot operator each time to access the member variable of the book and so in this case

05:39.790 --> 05:47.020
reading all five member variables and all you do is say your variable name that you came up with appear.

05:47.080 --> 05:49.950
And then Dortch whatever.

05:50.150 --> 05:58.980
So our book type really acts like any other type so we could even have you know an array of books and

06:00.710 --> 06:01.380
we want to

06:04.310 --> 06:16.810
Pook's this array of 10 books and actually define them all if you want like books Sphero dot you know

06:18.340 --> 06:19.030
Id

06:22.480 --> 06:23.110
right.

06:23.420 --> 06:27.020
So you could just kind of continue on like that.

06:27.590 --> 06:30.980
So it works just like any other variable.

06:31.000 --> 06:40.330
Or And again structures are a very important feature that help us with our design.

06:40.350 --> 06:40.900
OK.

06:41.050 --> 06:51.250
With our design of our data in fact just like we use functions to design or code we use structures to

06:51.250 --> 06:52.770
design our data.

06:52.960 --> 06:59.090
So often we need to define data in terms of the problem itself.

06:59.200 --> 07:09.140
So for example if we had a library we need books or a in searchers really do help great.

07:09.280 --> 07:20.080
Yes I hope you remember the picture that I showed at the start of the course where we had the transforming

07:20.130 --> 07:22.030
input and output.

07:22.030 --> 07:28.890
This is my mental model of programming while we've been focusing mostly on the kind of transform part

07:28.920 --> 07:29.990
of the algorithm.

07:30.000 --> 07:31.490
Part of it.

07:31.510 --> 07:37.960
Now in this section we'll be really focusing on the input data.

07:40.180 --> 07:46.240
Then we'll talk more about structures and how they can interact with functions in the next lecture.
