Next, we're going to look at members of classes and there are multiple different types of members,
the simplest one are member variables.
So member variable, let's call them properties alternatively, so you can either call them immoveable
or properties.
What will be an example, property for a person or a useful property?
Well, there are plenty.
But for example, age would be a very useful one.
So let's say we have the age.
And it can be no.
OK, so why would I create a variable called age that can be null for a person?
Well, the thing is that let's say we have the data of a bunch of people, so a bunch of users in our
database.
And for most of them, we don't have the age.
For some of them, we do have the age, but we have all of them, the first name and the last name.
So we want our program to run no matter if we have the age or not.
It should still work.
But if we have the age, then we maybe want to do something special.
We either want to display it on the app or on the website, or we could even go so far as to say Happy
Birthday or whatever if we don't have just the age, but the actual birth date, for example.
All right.
So this is an example of a member variable and we can have more of those.
So let's say I have a hobby here as well, which I'm going to define as watch Netflix.
So this is going to be the default value.
And this is an example of member variables.
Now let's have a look at member functions, so there is something called the member functions.
You can also call them methods.
All right.
And a member function is a function within a class as a member.
Variable is a variable within a class.
OK, so the member function that I'm going to create is just going to be a very simple one, which is
going to state the hobby of someone.
OK, so the only thing that it should say is front line.
My hobby is.
And then the hobby.
All right, so now the cool thing is we can go ahead and do the following, so let's say then, is that
a state hobby?
So let's see what will be printed onto our console.
So that we are initialised in your person object, first name doneness, last name Bunnytown, my hobby
is Netflix or is watch Netflix.
OK, so that is cool.
But now the problem is the hobby is the same for everyone.
So how can I change it?
Well, you can change it.
The only way you can go ahead and say then is that hobby should be something different, such as skateboard.
All right.
So maybe Dennis likes to skateboard.
And then if you run this code again, we will see that.
Now my hobby is skateboarding.
Maybe we should add something like to skateboard in here or something like that, but yeah, overall
the logic is the same.
OK, so what we now can do is and that is pretty cool, we can have an individual hobby for every single
person because every single person already has something that is unique to them or individual to them.
Unique is a little difficult to say because probably there are other John Doe's in the world.
But what we can say now is that the hobby of John should be something else, such as play video games.
All right.
So John Doe likes to play video games and now we can state his hobbies here as well.
So I'm just going to say, John thought state hobbies.
And if we run this, then we'll see that John's hobby is to play video games.
So this is just an example for why object orientation is so powerful or object oriented programming,
because we can now create blueprints for our classes, for our own data types or objects that we can
then go ahead and use.
So I create an object here, which is a variable or a variable is an object if it is of a type which
is a class.
OK, so in Scotland pretty much everything is an object.
Even the basic and generic data types such as Inson strengths are classes, so to see and you can see
that when you entered into keywords, you can see that this is a C, which means that this is a class
like.
So we create our own data types here.
We can change their properties and we can use their methods, which we can define ourselves as well,
so we can go ahead and create as many methods as we want.
In the last video, we looked at scope.
Why would we do that?
Because we're not using scope here.
And I said that we needed scope in here.
Well, because we are using first name here.
Right.
So in our in it in our initialised block, we use this variable name, first name.
And where does it come from?
Where it comes from our constructor here.
So this is our primary constructor.
And now in order to understand that, we need to create another constructor.
So what I'm going to do is I'm going to create a constructor here and a constructor is also a member
member constructor, to be more precise.
This is a secondary constructor.
All right.
So what I can do is I can now say what kind of variables I would like to have when somebody is creating
an object of type person who did that already here.
But I can have a secondary constructor where I say I want also to have the H.
So whenever somebody creates an object of person, they can also pass in the H and we will then use
it.
All right.
So I can go ahead and say, OK, give me the first name, give me the last name, but also give me the
H, which will be of type int.
Now, what we need to do here is we also need to add a column and then this first name, last name because
we are using the two.
OK, so I'm just going to put that in the second line here.
So this is our new constructor that we created, our secondary constructor, and now we can go ahead
and say that our H that we have there should be the H that is passed to us.
So I'm using this this keyword here twice.
So you might wonder what is does this.
Well, this this is a little easier to understand.
So what I'm saying is that the H that is given to the constructor when the object is created, should
be assigned to the age of this class, which means that it's going to be this one here, this H that
we created here at the top.
So to the member variable or to the property, it should be assigned to the property.
So that's what I'm saying with this.
OK, now this this here, it says that the first name and last name that we're using here in our secondary
constructor should be the ones coming from our primary constructor.
So they should be the first name that is passed here, John, and the second name that is passed here,
which is the last name.
So what we can do now is we can go ahead and give some more information.
So let's say I create a person which is called Dennis, and now I also pass to H.
So I don't only pass the first name and last name, but I also pass in the H.
So Dennis will now have an H.
So if I go ahead and say Dennis that H is equal to 32, then the initial H that was set will be overwritten.
So now if I want to print it, I can just do that print then.
Is H.
And maybe I should add a little more information then, is his then is that age old and here we need
to use brackets.
Years old.
So now let's run this again.
And that we are so we can see initialised the new person, last name then is Pennetta and then Dennis
is 32 years old and yeah, as a hobby of skateboarding.
All right, so now the cool thing is we can write own Prindle and statements when this other initialise
is called.
So here I'm just going to say initialize the new person object with first name, last name and then
and H.
So let's run this again.
So now it initialized the.
First constructor, so our primary constructor here, which was run by this initialise a block, as
we saw earlier, and then a trender constructor, which is our secondary constructor.
And I did this and eight year.
All right, so now another thing about the first name, so let's say I want to extend my state hobby
here, I want to say whose hobby it is.
So he just says my hobby.
But we didn't or we don't know who my is because, of course, here we know that it's John, but that's
because we programmed it.
But what if we only see the output so we will never know which person's hobby it is?
All right.
So in order to change that, I want to go ahead and to finally come back to where I started with this
whole thing.
I want to use first name here.
And I want to use it like this.
The problem is this doesn't work.
Why does it not work?
Well, because the first name isn't a variable that is visible because it's only visible within this
primary constructor here and it's also visible within our secondary constructor now that we have done
that.
So what I did with the H is what I can now do with the first name as well.
So please go ahead and try that, create a new variable.
You can call it a little different just to avoid confusion and then use it in your state.
Hobbie as well.
All right, so I hope you tried it and maybe the exercise wasn't 100 percent clear, I'm sorry for that.
But the idea is that we're creating a new first name here, which should be of type string, and it
couldn't be now.
So it can be empty.
And then we go ahead and in our in it so in our initialise that we assign to this first name, we assign
the first name.
So we do.
What we did here, so we take this first name and we store it within the member variable, so now we
can go ahead and use it in our state hobby.
So here I can go ahead and first name.
And I'm going to use a.
Backslash.
Apostrophe here and s just to say, for example, Dennis's hobby is Holly.
So let's run this again, and now we see Dennis's hobby is to skateboard, John Hobby, his play video
games.
So now we have a lot more knowledge about which object we have manipulated and which object does what.
All right.
So I think this is enough for this video, enough of different concepts that we're using.
I told you that the class can have multiple members and the ones that we saw are not the only ones.
Classes can contain multiple members here, constructors and initialize the blocks, functions, properties
nested in inner classes and object declarations, and we're going to see what those different things
are later on.
But for now, just know that there are multiple members of the class can have.