In this video, we'll learn how we can access class attributes and methods.
Of course, we want to create objects from our classes,
but we also want to be able to use and modify the object's attributes
and call methods for specific objects.
There are two ways to access class members.
We can use the dot operator
and we can use the member of pointer operator or the arrow operator.
Remember that some class members may not be accessible since we can hide them,
we'll see that in the next video.
We've already seen how we can use the dot operator when we worked with standard vector and string.
If we have an object,
then we can use the dot operator followed by the class member that we want to reference,
it's as easy as that.
For example, if I have an account object named Frank's account,
then I can access the balance attribute with Frank account.balance
and I can deposit a 1000 dollars into Frank account
by writing Frank account.deposit 1000.
In this case, we're accessing the deposit method.
Notice that the object is to the left of the dot operator,
so that method will be called on that specific object,
in this case, the Frank account object.
If we have a pointer to an object,
then we need to de-reference the pointer first in order to get to the object
and then we can apply the dot operator.
There's two ways we can do that.
Remember in these examples frank account is not an account object,
it is a pointer to an account object, which has been dynamically allocated on the heap.
The first way to do this is to explicitly de-reference the pointer
and then use the dot operator.
In order to access the object's class members,
we have to de-reference the pointer using the asterisk
and then use the dot operator to get the class member we want.
But notice that we have to use parentheses here
since the dot operator has higher precedence than the pointer de-reference operator,
and we want to be sure that we de-reference first.
While this syntax is explicit, it can be a little tedious to read and write.
So c++ provides an alternate operator called the member of pointer operator,
which is often referred to as the arrow operator.
It's a dash followed by a greater than sign which looks like an arrow pointing to the right.
This operator has the same effect as the first example above.
It will de-reference the pointer and then apply the dot operator.
As you can see,this operator is very intuitive to read and use and it's used very often.
That's all there is to accessing class members.
Let's head over to the IDE, and we'll access some class members for the player and
account classes.
Okay. So I'm in the IDE, and I'm in the section 13 workspace,
the AccessingClassMembers project.
What we've got here is we've got the same player class and account class
that we created in the last video with its attributes and methods, just like we did before.
And what we'd like to do now is
access some of these attributes and methods from
within our main, and we want to access them for specific objects obviously.
Now there's a couple of things we're going to do, and we'll discuss them in detail in a few videos from now.
But by default all of this information in the class is private,
and we don't have access to it. So let's make it all public for this video.
And in the next video, we'll talk about public and private.
So this is what I'm going to do here is just say public
colon for the account class and public colon for the player class,
that says that everything after that access modifier is public,
which means we've got access to it, no worries at all.
Okay. And as I said, we'll talk about public and private in the next video.
So what do I do? Well, let's create some objects.
Let's create a player object, and let's call it Frank.
And now what I want to do is I want to set Frank's name health and xp.
Those are the attributes for the player class.
Well, how do I get to them? This is where we use the dot operator.
Frank is an object, so we can say Frank
dot name
and we could just set it to Frank or anything else we like.
We could say frank.health
is a 100, let's say,
and finally, we can say Frank's xp
is, let's say, 12.
Okay. So it's really as simple as that. That sets those
attributes, the values for those attributes.
Now obviously, if I wanted to print out those attributes,
I could just do a cout Frank.health, and that's it.
And I'm going to get whatever was stored in there as a value.
So let's take a look at this in the debugger
and see what it looks like. So I'm going to put a break point right here on line 33.
And let's run this in the debugger so we can see what's going on.
First of all, you can see over here that frank is a player object,
that's its type.
And if I expand this, you can see it's all garbage right now, right.
So this is all garbage, and
let me hit next
and I just did. Let me refresh this one more time, you'll see that the string is now empty for the name.
Now what I'm doing here is I'm saying set the name attribute for the frank x
object to the string frank,
okay. And then I'm setting the health attribute for the frank object and the xp
attribute for the frank object to 112. So I'll hit
next a couple of times 1 2 3 times, I'll execute those three lines of code.
And what I'll do here is I'll refresh the screen so that we can see
that now we've got frank is the name,
the health is 100, the xp is 12. Exactly what we expected.
Okay. So
that's a really easy way to do this. Now remember, this is all based on the fact
that this is public. If this was private, you'd get a compiler error.
And I'll discuss that in detail in the next video.
Same idea holds for accounts. But how about
these methods here. I haven't implemented those methods.
So if I try to say something like frank.
talk, and i pass in a string to saw say hi
there or something like that,
this will compile just fine,
but it won't link because I haven't implemented this method.
How do we implement this method. There's a whole bunch of different ways to do it.
Let's do it the simple way. And again,
I'm going to do a whole video on methods and how to implement them.
But for now, just to get this to work, let's put in a method block here.
And just like we've always done, I'm going to say cout
what's my name? Well, my name is name.
So I'm going to say name.
I'm going to put another
insertion operator, and I'll say
says followed by
the text to say, which is being passed into me
followed by an endline.
That's it. Now I've implemented that method.
So now when this code executes right here frank.talk hi there,
it should say frank says hi there.
Okay. So let me stop this, and let's run this again.
And again, we'll run this in the debugger. And since we have some output, what I'm going to do is
put that output window right over here so we can actually see what's going on
in our output window.
Okay. So let's do this again. I'm going to select next.
I'm on line 34 now, line 35, line 36.
Now I'm going to execute line 36. And at this point,
my frank object has the name frank health 100, xp is 12.
Now what's going to happen is we're going to call the talk method for the frank object,
and control will be transferred right over here to this method we just wrote,
we're passing in this string hi there, and it should say frank says hi there,
and it should display it right out here. So let's take a look.
That's exactly what happens, frank says hi there.
Okay. So what's neat about this is that since I'm calling the talk
method for the frank object,
these attributes are available to that method, right.
So I don't have to pass in the name of frank because I've already got it, I can just simply say name.
As far as this name is concerned, remember, we we talked about scope a while back.
Well, we look for the definition of this variable
inside the class first. If it's not there, then we go global.
But we're going to look for it here. There it is. That's the one we're going to use.
Okay. So there you go. That's a real simple example of calling
some a member method, really, really easy.
Now how about if we had a pointer. Let's try that.
So let me stop this, and we'll do the same kind of code. We'll write it with a pointer,
and I'll leave this window a little bit smaller, so we can see. So let's say we have a player
and it's a pointer this time called enemy which is a pointer to a player.
And I'll just instantiate it right here, so I'll say it's a new player.
Okay. So in order to get to that player object,
I need to de-reference the pointer, right. Remember,
enemy is not a player object.
Enemy is a pointer to a player object. So in this case,
there's two ways to do it. We can de-reference enemy.
Okay. So what I just did was I de-referenced
the pointer. So now I've got the object.
And I can say enemy.name
is enemy,
simple as that. I could say enemy
.health is 100.
And I could do the same thing for xp, but rather than use this syntax
where we're explicitly de-referencing the pointer and then doing the dot operator,
let's use the arrow operator here. So what we can say is enemy
followed by the arrow operator.
And then we can say xp is, let's say, 15,
right. Now how do you call a method here, same way. We could say enemy
arrow. So what I'm going to do is I want to de-reference that pointer
and use the dot and then I'm going to call talk
and say
I will destroy you.
Okay. So let's try this now. Let's walk through this one more time.
So we'll debug this.
I'll move this over here real quick,
and let's step through the program.
Right now, we're on line 33, that's where we've got the break point.
So we're creating the frank object.
We're setting the name to frank, the health to 100 and the xp to 12.
And then we're going to call the talk method on the frank object,
which should display frank says hi there.
And that's exactly what happens over here in the output.
Now we're going to create this enemy, which is a pointer to a player.
Now it's important that you see that. You can see the type here.
The debugger is really helping you out here. You can see frank
is a player.
Enemy is a pointer to a player.
You can see that the debugger is really helping you out there with the type. Okay. So at this point right now
that enemy is garbage, right. We've got garbage data in the pointer.
As soon as i execute line 39, now it will be pointing to a real player object.
So I'm going to sit next.
Now we've got enemy pointing to a player object. And if I come over here,
you can see the name is junk, the health is junk and the --
sorry,the name is empty, the health is junk and the xp is junk.
Okay. So what do I do here?
I want to de-reference enemy and then get the name.
So I want to follow the pointer and get the name.
Okay. We can also use this area right here with the watches.
So I can add a watch.
And you can put whatever expression you want. So i just want to see this expression right here,
de-reference enemy, that's the object.
So when I add that, you can see I'm seeing the same thing that I'm seeing here.
Now for some reason, this you need to refresh, this you don't.So it's kind of nice to have these here.
So let's walk through this. De-reference enemy that means go to the object
that I'm pointing to and set the name to enemy.
Okay. So let's execute that line of code.
Notice how enemy shows up here.
Doesn't show up here unless I refresh,
but now it does.
Then I want to set the health to 100. So I'll do that next.
Now the health is a 100,
xp is 15. But now we're using the arrow operator. But it's all the same,
xp is 15. And now I want to call the talk method for the enemy object,
but I want to do it through the pointer.
And when I run this, it should say enemy says I will destroy you, so let's try that out,
and that's exactly what it says. Enemy says I will destroy you.
Okay. So that gives you an idea of how some of this works.
What we'll do next is let's update this a little bit.
We'll do that quickly because this video is getting a little long now.
And what we'll do is we'll do the account.
Okay. Same idea
except here I'm going to say something like
what do I want to do when the user
passes in a number to deposit. Well, what I want to do is just say something like
balance plus equals bal, right,
just something really,really simple like that I want to increment the balance by whatever they passed into me.
And for withdrawal, I just want to say balance
minus equals bal.
And while I'm at it, I want to also say cout,
something like in deposit,
just so I know that I'm in there, so we could see this being called.
We'll do that, and we'll do the same thing for withdrawal.
And again, just so we can see these things are being called.
Now we're defining these methods right in here, right inside the class as I said,
there's other ways to do this.
And I'll show you what those are in a couple of videos. But let me just change this, so we can
understand how to access this. That's it. So now let's create
right up here.
Let's create an account. So we'll say account,
and we'll just say frank account,
right. That creates an account object.
Now I can set the name and the balance of that account. These are public attributes right now.
So I could say frank account,
and I'll just say equals frank's account.
That could be any c++ string.
And then I'm going to set the balance. So I'm going to say frank's account.balance,
and let's say that there's 5000 dollars in there.
Okay. That's it.
Now let's say I want to deposit a 1000 dollars into that account.
I could say -- again, you need the object that you're working on has to be
to the left of the dot operator. So it's going to be frank account.deposit,
and I'm going to deposit a 1000.
Okay. And then we'll say frank account.withdrawal
and let's say we want to withdraw 500.
And that's it. And I'm forgetting semicolons because I've been working with a programming language all day,
that doesn't need them. So I'll put them in there. Hopefully, I didn't forget any other ones.
Okay. So that's it. Let's put a break point here, and let's try running through this.
So again, we'll go to the debugger
and frank's account equals --
oops of course, frank accounts.name.
I need to set that specific attribute
and frank account.withdraw
500.
I have an extra paren right there.
Okay. Let's try that now.
Okay. Perfect. So let's put the output window over here on the side,
and let's walk through this. So I'm at line 33 right now.
I'm creating frank account. So I'm doing that right now.
Now I'm setting frank accounts name to frank's account.
We'll do that and the balance to 5000.
So let's take a look at frank's account right here, we'll refresh.
There's frank's account, and the balance is 5000.
Now when I call the deposit method,
it's going to call this method right here for that frank account object
and increment the balance by a 1000.
Okay. So let's do that,
and we should we should also get an output statement printing out over here.
There you go in deposit. I'll refresh frank account again.
Notice I've got 6000 in here now.
When I call the withdraw method, this should go down to 5500,
and it should say in withdraw. So let's do that one more time,
and we'll refresh one more time.
There's 5500, exactly what we expected and the in withdrawal kicked in.
Okay. Same idea here with a pointer that we did with the player object. I won't do that
because the video is getting a little bit longer now, but I'll let you guys try that out.
You can create Mary's account, which is a pointer to an account,
and then you can access the attributes using this syntax or the arrow operator.
Okay. So if you have any questions, please drop by the forum and let us know.