OK.
Welcome back to another lecture in this lecture, we're going to continue on talking about object oriented
programming, and we're going to actually continue on with this car class using as an example.
So this lecture will have two new kind of concepts or sub concepts within the realm of classes and object
oriented programming that we're going to go over.
And the first one that we're going to talk about is something that's not a huge topic, but I want to
mention it, and that is something called CONT-.
You might have seen this already and already know what concept means, but just in case I'm going to
go ahead and go over it and also tell you how it pertains to functions.
So you might have seen something like this as a constant, then a variable type.
Right?
Sorry, a data type.
And then we have a variable name and something like this where it says contact and and it really can
be any data type.
And then we say, here's the variable name calling.
I won and we say it's five.
The word con stands for a constant.
And what it means is that this integer I one should always remain five.
It should be basically read only after that.
We can print it out.
We can see what it is, but we should not make changes to it.
So if I was to try something like this and I say I equals seven, let me go ahead and save this and
I'm going to compile it in here.
Let's say G Plus plus R.S.V.P. Dash.
Oh, card out
and notice that it gives me an error rate here.
So it says assignment is this error assignment of read only variable i1.
So it's telling us that this i1 is really only it's really read only we're not allowed to modify it
yet.
We tried to modify it here on Line sixty eight and this can be anything like I could have this piece,
you know, constant flow and it could be five point seven.
And then here I just try and change it to like 7.9 or something like that.
It still is not going to work.
It's going to yell at me and say it's still in assignment of re, the only variable I won.
So that's what constant means.
And why would you use constants?
Well, sometimes they're a little more efficient, you know, with compilation and all that stuff,
because it's just knowing that this is going to remain the same.
The whole time kind of makes everything a little more optimal.
So they're useful.
If you can make something konst and you know that it's not going to change, then it might be a good
idea to make it constant.
But so that's kind of a little recap, if you might have seen that before.
If not, now you know what cost is as far as basic variables and data types and stuff like that, so
you can make pretty much any type.
This can be anything here, this highlighted section, and you can put a cost in front of it.
So it also can be used with functions.
And what I mean by that is that when you put context after a function like, for example, I can say
and get odometer cards.
So this is our member functions area, remember?
So what I'm doing here is I'm putting this contest.
And this is the prototype that means I also need to put it down here.
So we have get odometer, and I would need to also put a concert right here after the parentheses,
just like I did up here.
What it means is that if we put this here, it's saying that this function is not going to modify the
data of the object that is calling the function.
And what is the object that's calling the function?
Well, let's look at an example.
We made a car object in a car to object, right?
Let's take it.
Let's take a look at car for an example.
So here we have car dot, get odometer.
This is the function get odometer being called on.
And I say on because there's the dot, it's car dot yet odometer.
So get odometer function is being called on car.
So let's go back up here.
What I was saying previously is that Konst means that the function get odometer should not modify the
data of car in this example, and it doesn't write it's a getter or what we would call an accessory,
right?
It's not a mutineer or setter.
It really just gives us the value of odometer, it's not changing anything, it's just giving us back
whatever stored in odometer.
So this is something that's totally fine and I can go ahead and say this and run this, and you notice
that we don't get any error if I compile it.
What if I was to put a cost on something else that does modify it, let's see what happens.
So I go ahead and I put cost here and I say, Oh, that pain is not going to change anything, I promise.
And then I'll go down here and I'll say, OK, let's go ahead and cut the cost on here.
So it matches.
And then I say this, I go ahead and.
Compile it.
Oh, look at this, we have some errors, so what do these errors say?
Well, you notice that it's actually yelling at this, yelling at us for this code right here.
Where is this paint color equals color?
It says passing constant string as this argument desk discards qualifiers.
This is a kind of a fancy, complicated way of just saying that, hey, you said you weren't going to
change any of the data that has to do with the calling object and calling object is actually referred
to as this like literally the word this.
That's why it's here in the air and the arrow.
So that calling object, we were not supposed to change its data and we said we wouldn't.
Yet right here, compilers noticing that that is what we are doing.
Painkiller equals color.
So that is an issue and we cannot have that.
So we're going to have to take this out now.
You don't want to put a cost on anything that is changing the data because that is not a conscious for
it's for something that doesn't change the data, right?
So we can put it here because it just returns odometer and it's always just going to return odometer.
And it's totally fine.
If we were to like, modify odometer like later on, like, I could make some function here.
I could say like and said Odometer, and I'm not going to make this cost.
Let's go ahead and just say it.
And it's going to call a reset or something, so we can say with this, get a whole another thing here
that so we don't want to return it and say this will be void and then I'll go down here and we'll just
say boy car sets odometer.
And here was an reset.
That's what we're going to reset it to.
And we just say odometer equals reset.
So I could do something like that.
And let's just say we call get odometer.
And then maybe here I'll send you a car set odometer.
And I just say like eight hundred thirty seven or something like that.
And then we go down here and we also say, car, it will get odometer.
OK, so.
Now I'm showing you that I have a set of downers, so we are changing the odometer value, right?
So we said that this was constantly get odometer.
Yeah, I'm changing it in this other function, but let's see what happens.
So go ahead and compile it, and it gives a little bit about this, so it says it does not match any
class.
That is because.
I did set paint.
I think I put a constant road to tears.
So.
Oh, yeah, I this is so.
So let's go ahead and save this.
All right.
So compile again, and it's fine, and why is it fine?
It's fine because we are not saying that, said Odometer is concerned this would be a problem, right?
But we're just saying that get odometer.
So we're saying that this function specifically is not going to change any data like it's not going
to change odometer or any other data, for that matter.
This one is changing.
And so we're not going to put const.
So just wanted to introduce that.
That's kind of the first thing I wanted to show you was concert in general and then cars being used
with member functions of a class.
So the next thing that we're going to go.