Hello again! In this video, we're going to go over classes. A class is a compound data structure.
Classes can have member functions as well as data members.
So this is basically how we do object oriented programming.
We have data objects which know how to manipulate themselves. And we do that by calling a function.
And these member functions can have any names we like.
They can do anything we want and we can choose when to call them.
So these member functions define how the objects of the class behave.
And when we call a member function, it performs an operation on the [object].
By default, the class members are private, which means they can only be accessed by objects of that
class. Although you can actually access the private member of a different object of the same class. That's
needed so you can copy data.
You can make a class member public, which means it can be accessed by any code in the program.
So if you have a public member function, it can be called by anything that has an object.
The public members are the interface of the class.
So this represents the class to the rest of the program.
So this defines everything that the class can do.
So that's the interface of the class, the private members provide the implementation of the class,
so the actual details of how it does it.
So as an example, if you have a phone, then you have the screen on the phone; you can interact with
the phone, you can make it do different things.
But the actual details of how the phone takes a phone call or finds things on the internet or responds
when you tap on it, that's all hidden away from you.
And finally, there's also a struct which is the same as class, except all the members are public by
default. So normally, you would use a struct when you have some related data items that you want to keep together.
So normally you wouldn't use the functions for the struct unless you're writing a throw-away class and you're
being a bit lazy :)
So for example, if you get some data from a database, you could have a struct, and each member of
the struct would represent a column in that database.
This is the Test class. You're going to be seeing rather a lot of this! We're actually going to implement the functionality
of a string.
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
everything to that.
So this has private data members.
So these are the actual details of how the Test class stores its data.
And then there'll be some public member functions.
So we could use these to get the data from the strings,
for example. Or we could perform some more interesting manipulations on it.
A member function is actually implemented as a global function.
So it's just a global function, which has the name of the class in front of it.
So if you have a function called func, it's going to be something called test double colon func
in the actual program binary code.
When you call the member function, you have to provide an object, and that object will actually be
passed by address in a hidden argument.
So if we call func with arguments one, two and three on an object test, it'll actually have the test
object passed by address as the first argument to the function call.
And then the arguments that you give will come after that.
(Up here again!) In the function body, you can access this object through the pointer.
It's called "this", so "this" is a pointer to the test object.
And if you dereference that, then you can gain access to members of the object so you can get data from data
members and you can call member functions.
So in the body of this function, you can write this, arrow operator i , equals one.
And that will dereference the this pointer.
That'll get the test object and set its i member equal to one.
Normally, we don't bother writing the explicit dereference.
We can just do i equals one, and that'll do it implicitly.
And the compiler will work out that we mean to dereference the i member of test.
And the pointer "this" will be the address of the test object.
OK, so that's it, just a nice short one.
I'll see you next time, but meanwhile, keep coding!