WEBVTT

00:01.740 --> 00:09.200
Hello again! In this video, we're going to go over classes. A class is a compound data structure.

00:12.480 --> 00:15.570
Classes can have member functions as well as data members.

00:16.230 --> 00:19.350
So this is basically how we do object oriented programming.

00:19.650 --> 00:26.280
We have data objects which know how to manipulate themselves. And we do that by calling a function.

00:27.480 --> 00:30.060
And these member functions can have any names we like.

00:30.840 --> 00:34.050
They can do anything we want and we can choose when to call them.

00:35.640 --> 00:39.270
So these member functions define how the objects of the class behave.

00:40.530 --> 00:43.650
And when we call a member function, it performs an operation on the [object].

00:46.580 --> 00:52.280
By default, the class members are private, which means they can only be accessed by objects of that

00:52.280 --> 00:59.960
class. Although you can actually access the private member of a different object of the same class. That's

00:59.960 --> 01:01.850
needed so you can copy data.

01:03.290 --> 01:08.200
You can make a class member public, which means it can be accessed by any code in the program.

01:08.210 --> 01:12.200
So if you have a public member function, it can be called by anything that has an object.

01:21.240 --> 01:22.980
The public members are the interface of the class.

01:23.490 --> 01:26.820
So this represents the class to the rest of the program.

01:27.270 --> 01:30.570
So this defines everything that the class can do.

01:31.770 --> 01:37.010
So that's the interface of the class, the private members provide the implementation of the class,

01:37.020 --> 01:38.970
so the actual details of how it does it.

01:39.780 --> 01:44.370
So as an example, if you have a phone, then you have the screen on the phone; you can interact with

01:44.370 --> 01:46.690
the phone, you can make it do different things.

01:47.820 --> 01:54.450
But the actual details of how the phone takes a phone call or finds things on the internet or responds

01:54.450 --> 01:56.800
when you tap on it, that's all hidden away from you.

01:58.810 --> 02:03.550
And finally, there's also a struct which is the same as class, except all the members are public by

02:03.550 --> 02:10.870
default. So normally, you would use a struct when you have some related data items that you want to keep together.

02:11.260 --> 02:16.390
So normally you wouldn't use the functions for the struct unless you're writing a throw-away class and you're

02:16.390 --> 02:17.200
being a bit lazy :)

02:18.310 --> 02:22.660
So for example, if you get some data from a database, you could have a struct, and each member of

02:22.660 --> 02:25.720
the struct would represent a column in that database.

02:30.840 --> 02:37.440
This is the Test class. You're going to be seeing rather a lot of this! We're actually going to implement the functionality

02:37.680 --> 02:38.520
of a string.

02:40.140 --> 02:46.200
But as we don't know how to do that just yet, we're going to start off by using the library string and delegating

02:46.200 --> 02:46.920
everything to that.

02:48.360 --> 02:50.440
So this has private data members.

02:50.940 --> 02:55.300
So these are the actual details of how the Test class stores its data.

02:55.800 --> 02:57.740
And then there'll be some public member functions.

02:57.750 --> 03:00.180
So we could use these to get the data from the strings,

03:00.180 --> 03:05.100
for example. Or we could perform some more interesting manipulations on it.

03:11.430 --> 03:14.880
A member function is actually implemented as a global function.

03:15.390 --> 03:18.510
So it's just a global function, which has the name of the class in front of it.

03:19.050 --> 03:23.940
So if you have a function called func, it's going to be something called test double colon func

03:23.940 --> 03:26.730
in the actual program binary code.

03:28.620 --> 03:33.000
When you call the member function, you have to provide an object, and that object will actually be

03:33.000 --> 03:35.490
passed by address in a hidden argument.

03:36.270 --> 03:44.700
So if we call func with arguments one, two and three on an object test, it'll actually have the test

03:44.700 --> 03:48.480
object passed by address as the first argument to the function call.

03:48.870 --> 03:50.970
And then the arguments that you give will come after that.

03:54.790 --> 04:00.550
(Up here again!) In the function body, you can access this object through the pointer.

04:00.910 --> 04:05.440
It's called "this", so "this" is a pointer to the test object.

04:07.170 --> 04:12.600
And if you dereference that, then you can gain access to members of the object so you can get data from data

04:12.600 --> 04:14.490
members and you can call member functions.

04:15.240 --> 04:20.190
So in the body of this function, you can write this, arrow operator i , equals one.

04:20.940 --> 04:22.800
And that will dereference the this pointer.

04:23.360 --> 04:27.780
That'll get the test object and set its i member equal to one.

04:28.560 --> 04:32.250
Normally, we don't bother writing the explicit dereference.

04:32.550 --> 04:36.270
We can just do i equals one, and that'll do it implicitly.

04:36.270 --> 04:40.320
And the compiler will work out that we mean to dereference the i member of test.

04:42.730 --> 04:46.660
And the pointer "this" will be the address of the test object.

04:49.230 --> 04:51.540
OK, so that's it, just a nice short one.

04:52.350 --> 04:55.290
I'll see you next time, but meanwhile, keep coding!
