OK, so moving on with classes here, you notice that I have the same class that we were discussing
in the last lecture.
I have it here in Visual Studio Code.
So here is the class definition here, and this is our blueprint.
You notice that we have the private member variables section and public member functions section.
So let's see what's below that.
Here we have something we also talked about in the last lecture, which is the actual implementations
of the member functions.
So remember the scope of resolution here.
So this is the scope resolution operator and we are using the name of the class right before it, right?
And then afterwards comes the name of the function.
So this is kind of like one big name.
Here we have the return type of each function is going to be coming before the car, colon, colon.
And these are all of our functions that we discussed.
So down below all these functions, I am putting my main function here.
And so now what we're going to discuss is what to do when we're creating an object of the car class.
So now we want to make a specific car, right?
We have the blueprint up here and we have the function implementations, but now we need to make an
actual car object.
It's going to be just like how it made a structure.
So I can say car capital C, right?
Because that is what we named our class.
In one of my call, this lower case, the car has the variable name so simple as that I make a car and
then so let's try something interesting now I have a car, right?
Well, let's go ahead and use some of these functions, right?
Let's do get odometer and get fuel so I can do see see, our car is my variable right and then we're
going to use a dot just like the struct in real.
In the struct, we use a dot to access the members.
Well, here I put a dot and you notice that it gives me the option to call any one of these functions
here.
So I have drive car, get fuel.
Let's go ahead and get odometer.
That's a function call.
So I need to put some parentheses right here.
You can think of this as like a string dot, dot string dot length or something like that.
Another interesting thing to talk about is the fact that, you know, string.
I'm using it as an example, but string actually is an object and it's actually a class.
There's a string class.
And when you're using that standard namespace, you know, it's kind of just par C++ in general.
You can just use strings, you have access to that class.
And so therefore you can use the member functions of that class.
One of the member functions is length.
So when we make a string like string s or something like that, we do escort length.
It's similar to how we're doing car dot, get odometer.
So I just want to point that out.
That string is a class, so maybe that can kind of tie together some things that you've seen so far.
So this kind of interesting let's see what would print out, I mean, we haven't really said what the
odometer is when you make a new car, right?
So I'm going to be interesting seeing what prints out.
Put it in line.
And then let's also see our car door get fuel.
So this is going to return the fuel to us.
And that's all I want to do.
I'm just going to print these two out and that'll be the program.
So I'm going, go ahead and save this.
And then here I have a terminal.
Go ahead and compile.
And then I'm going to run this and let's see what we get as output.
OK, so supposedly we have.
For what is this four million, two hundred one thousand three hundred thirty nine miles on our new
car?
That doesn't seem very good, right?
And then we have, you know, this crazy number right here as our fuel.
So why do we have those numbers?
Well, this is something that you might already know, but what's happening is that this is garbage.
This is some of those garbage values.
This is just coming out of memory.
We've created these variables fuel and odometer, where we make those we define to them here.
Was there float fuel in odometer?
The problem is, is that we never initialized these variables.
So when we make a car object, it basically when it tries to call get odometer, which it does, it
doesn't try, it does call it odometer, it goes here, says return odometer.
We get the value of the odometer back here.
But what is the value of odometer?
It's just some random trash from memory just picks up wherever that variable is sitting and memory is
just going to pick up whatever's there.
And that could be anything, you know, and it's just interpreting that garbage as these bytes as this
decimal number here because of the data type, right?
It's an integer du type.
And then we have a float in that flow is just some crazy number two.
So what do we do about that?
How do we initialize these variables?
So that is something we're going to talk about next.
Now you see that we have created a car.
And this is the syntax is similar to kind of a struct.
But what if we wanted to say exactly what a car is going to start out with as far as the odometer and
the fuel one odometer?
We know what that should start out as it should start out as zero right, like zero miles for a new
car.
So what we can do is actually make something in the code up here.
And this is something pretty interesting, kind of unique to classes, it is called a constructor.
And the reason is called a constructor is because it is what gets called when you build the object.
So constructing is kind of like build, the same thing is building, right?
Those things are kind of synonymous.
So here is where you create a car object right on this line right here.
So you can think of this when you do this, it is going to call a special function called a constructor.
And since we're calling it on this line here, you know, we wouldn't put like a dot right away for
a function because we need to have this style syntax that creates the car variable, right?
So we're not going to put a dot when C++ sees this, it's just going to automatically call our constructor.
So the question now is how do we make a constructor and what is it?
Well, it is going to be here in our public methods function or public member functions area, but it's
going to look like this is just going to literally be the same name as the class with some parentheses.
And so this is an empty default constructor, is what we call this.
You're going to call the name is basically like a function like drive car, except the name of the function
is the exact same as the class name, and you do not put a data type.
There is no void.
No in no float.
Don't put that.
Just put the name of the class with some parentheses here, and they don't have to be empty.
We're going to get into that in a second.
But right now, we're going to leave them empty.
And so this is a basic default constructor, and you can put it here in the public number functions
area.
So I'm going to have to actually make an implementation of this, just like I did for these functions,
so let's go down here and see what that's going to look like.
I'm going to put it above all of these other functions in terms of top to bottom.
And I'm going to go ahead and put this scope resolution just like this function car call and colon.
So I can actually copy that and I can go over here and I can paste that so I don't have a car.
And then I just put the same thing again, car, because that's the name, right?
That's the name of the constructor.
It's just car.
It's the same as the class name looks kind of weird because we have Capital C car call and call colon
and then just Capital C car coming right after it.
But that's how it's supposed to be.
Syntax was so I'm going to make it just like a function.
I'm going to put some brackets here.
And so this function is constructor is going to get called right here when we make the object.
This is us making the car and so, you know, once we make the car, it's going to be like, Oh, you're
making a car, I'm going to jump and up and try and find this constructor and then I'm going to run
whatever code is in here.
So what do we want to put in here?
Well, the constructor is all about initializing our private variables.
There's other stuff you can do in the constructor.
I mean, technically, you can do whatever you want, but one of the main things that we're going to
start doing is initializing these member variables that we're in this private section here.
So what do we want for fuel?
Well, fuel should probably start out as something other than zero.
So I'm just going to say I don't know how many gallons, let's just say like 12 gallons or something
like that.
And then I'm going to say odometer equals zero because we're going to start out with no miles, right?
And then we can just put some default paint color.
We could say not string sorry and we could save paint color.
Let's say that all of them kind of.
Come off, you know, without really having paint, there's something, you know, maybe maybe the default
color is just metal or something like that.
We just say that and then model and we'll just say this is blank.
Well, you don't have to put metal or something.
We can just put a blank string here for Painkiller as well.
So that way, at least there is something there for the painkiller model, even though it doesn't really
have any meaning, it's just an empty string.
Quite often in the constructor, you'll find yourself initializing these member variables to like the
most basic value you can think of, so integers will often be zero.
But this is actually a float like that, so let's put a four, 12, 12 point zero.
And we got to put semicolons here, right?
So.
A lot of the things that you're going to see in here are going to be basic values, so like we're saying
12 for fuel, just because it doesn't make sense to really start out with a car having no fuel when
you want to drive it off the lot, right?
But a lot of times floats will be zero point zero.
Integers will be zero.
Strings will be empty.
String so on and so forth.
But they don't always have to be.
I'm just saying that that's kind of a common thing if you don't really have a specific value in mind
first, just to initialize it to the most basic thing possible.
So we're initialized in all four of our member variables here.
So let's go ahead and save this and recompile it now and see what happens.
So notice I've prototyped it up here, implementations right here and then down here we are still instantiating
our car.
So just another reminder on that when we make an object of a certain class, it's called instantiating
it.
We make an instance of the car class, right?
So let's go ahead and test this out and see what happens.
So I'm going to go ahead and save this, and then we're going to go down here and we compile it and
then run.
All right, so what do we see now?
Well, the get odometer returned to zero because we have zero miles in the jet fuel is 12 gallons.
So this is what we start off with now.
The basic car, so you notice that these values get odometer is just getting the odometer and get fuel
is just getting the fuel.
And why are they zero in 12?
Well, because when we created the car and where do we create the car right here on Line 50, right?
When we created the car, what it did was they called this constructor code right here and it set fuel
to $12 zero in the painkillers in model here to empty string.
So that is a default constructor.
This is when we have no arguments being passed to it, we have no parameters that we've defined.
The thing is is that if we want to, we can say when we make a car, how many miles we want on the car
and how much fuel we want in the tank and what color car we want and what model car we want.
And that's kind of normal, right?
Maybe not how much fuel is in it, but it's normal to say, OK, well, I want a used car that has,
you know, about 50 thousand miles and I want the color to be white and I want the model to be this,
you know?
So that's something pretty normal, so why don't we implement a constructor that can handle stuff from
the user?
It can have a base line car that has the parameters that the user describes if the user can pass those
as arguments.
So I'm going to add one more thing here.
Instead of just model, I'm actually going to say I'm going to have an extra thing here, and I'm just
going to call this like a.
A strange branch, I call it.
Yeah, I guess I'll just call this like the brand of car.
And then I'm not going to have a brand or any of that stuff.
So what are we going to do if we want the user to say this?
So let's go ahead and have the user choose not the.
Fuel, because that would just be up to where, you know, the dealership or whatever, but let's say
paint, color, brand model and odometer, so we're going to have three strings.
And so let's just say the string paint a steady string and we'll just call this.
This is the brand.
Let me just call this brand and then I'll say a Stevie string model.
And then and then the last parameter here will be ends in this will be miles.
So we'll see how many miles they want in the car, and we'll say all this stuff, so we notice that
we have a similar named parameters here to this stuff here like brand and model.
So what I'm actually going to do and this is just something that I like to do quite often, is I like
to put a little underscore in front of these.
So I'm actually going to name these with a little underscore.
And this is something this is not just me.
A lot of people like to name their member variables like this actually applies to some other programming
languages as well.
So what we're going to have to do then is put underscores one of returning these things, and I'm going
to have to update this.
So it's going to underscore Odometer.
And then this, of course, will be these I can handle them because I'm going to change that.
And let's see Painkiller.
Phil?
Deal.
All right, cool.
So I have changed these now, and I'll fix those in a second.
So we have these parameters now and we're putting this is remember, this is our prototype here, so
we have to define the parameters in the prototype as well for the constructor.
And of course, all these other functions.
So let's go down here and handle the implementation.
So.
We're going to copy and paste all of this in here, so I'm going to go ahead and copy this.
Let's go ahead and put it here in our implementation.
The parameters?
And so what I'm going to do, remember our field stays the same with the rest of these are going to
get set to these things that get passed now as arguments to the constructor.
So what I'm going to do is I'm actually going to
leave feel the same, but I'll say odometer equals miles.
And then paint color.
Going to be able to paint that the user's passing model is going to be equal to the model that these
are passing.
And let's go ahead and also put the brand here and we'll say brand equals brand.
Cool, so now the user can kind of specify what type of car they want to start out with.
So let's go ahead and check this out.
Let's see how we're going to do it in the code down here.
So what we can do is now we just have this car was like this without any parentheses, right?
Even though that was kind of a function we were calling for, the constructor is still worked without
frenzied, but now we're going to put parentheses because we want to specify the arguments for our car.
So let's see patent brand motto miles.
So let's say we want the paint color to just be like blue.
We want the brand to be like, I'm just going to say something like silver.
I like serious.
And then let's say the model will be something like the X or something like that or.
Yeah, and Impreza or something crazy not to spell that.
Others say outback or something like that.
And then we have.
The was it Miles, right, and so we want to use car and we say, well, we just want something like,
you know, forty thousand miles or something like that.
So let's go ahead and try this out now.
So now we have.
All right, get odometer.
And we have our jet fuel, let's as some other stuff, so let's say see out car dot and now see we we
need some more accessories.
So let's see.
We have the.
Painting toilets out of some a couple more minutes in here, so we can test out our our color and brand
and stuff, so let's see.
Steady string and we will say get.
Get paint us give.
Get paint.
And now, let's say a steady string, and I get brand as steady string and get model.
And yeah, that should do that should do for us.
We also have this set paint here, so let's go ahead and test it out as well.
So these are going to need to be implemented now.
So we have this get paint, get brand, get model.
So I'm going to go down here.
Well, let's do it after sit paint, so we will say as TV's string car and colon get paint.
And so this should just return a paint color.
Oops.
Let's go ahead and do that in the other ones.
So this one will be get model.
And so I will just say return model.
And then let's say a steady string car gets brand and we will go ahead and just return France here as
well.
And I believe that was all three.
Right, so.
We have let's go check our function here.
So get paint, get behind your model, those are the new ones.
All right, so let's go ahead and test out here then, so we'll say see out card, get paint.
And then let's go ahead and do a function call right here where we say a car got set paint and now we
change, let's say, like, Oh, I'm going to repaint my car unless paid in black, and then let's go
ahead and do another Seattle card dot get paint right to show how that's changed.
Right, because our get paid here are set paint, we take a string, right?
So sipping here takes a string and then it says the paint color to that color.
So if we can't get paint again, we won't have that new painkiller.
This is a cool thing about classes.
These member variables are shared across the class, so.
We defined all of these member variables here, right in their private private means that down here
in Maine, we don't really have, you know, an access to that.
So but here when we put these functions here that aren't Maine, but they say car colon, colon, the
car colon, colon is giving us access to the private data members.
So the interesting thing is if we change paint color, it stays the same across this whole class.
So when we call this method again, it's going to get the updated paint color right, so it saves the
state of the paint color.
So let's go ahead and prove this right.
So let me go ahead and save it, and then I'm going to compile it.
Let's run it and let's see how this works out.
So what we do get odometer, it started out at 40000, right?
So when we print this out, it shows 40000 wheels.
The next thing we print out car to get fuel.
We never set our fuel explicitly here.
When we made the object right, that was not for the user.
The default constructor set the fuel to twelve point zero.
So we go ahead and we print that out and we have 12 here, and then we do a get paint well.
The original paint that the user specified was blue.
So when we call it Clinton, says blue, then we say car got set paint, which sets it to black.
What happens with this?
This gets this black right here in this function call it gets sent up here to set paint.
So black is here.
And then it says painkiller now equals black.
The next thing we're doing is doing a C out of get paint, right?
So we see our car get paid.
Go up here and we do get paid paint Painkiller has been set to black.
This state is saved across this whole class.
This is considered as part of the class.
Be here because as car coal and coal in scope resolution operator here.
So that's why we see that it now prints black when we print it out down here.
So pretty cool.
Now we know not only how to make a constructor, but we also know how to pass arguments to it and initialize
the values to the user's choice kind of where they pass these arguments.
So another important thing that we've been doing is using these things called getters and setters,
which would be get paint and set paint, get odometer, get fuel, get paint.
So these are getters when it says get and you also can refer to getters and setters as accessories and
haters.
So a getter is also an accessory, and a setter is also a mutate or mutate.
Or just mean to change a setter is something that changes the color, right?
So we have set paint changes the color to black, but get paid just gets you.
Whatever the current state of paint, color is right.
We have paint color here and whatever the current state of paint color is, when we call get paint,
all it doesn't just return you whatever value is currently stored in there.
This access, this mutate or here or this center is to change is for change it, for modifying.
It makes a modification to this variable right and it makes the modification based on whatever color
gets passed as an argument because you see this parameter here.
So it's pretty cool.
Another cool thing is that we're not limited to making one car, we can make as many cars as we want,
so let's go ahead and make another car.
I'm actually going to just call this car to car car two, and I'm going to say this one was white and
this is a Ford.
And it is.
Let's see.
The model is like a Ford Fiesta.
And then we have the miles are only ten thousand.
How about that?
So let's go ahead and print these separate things out.
So what we can do is we could see out and we can say, let's say car one.
Uh, let's say car one miles.
And we'll say car get odometer.
And now we can actually say a car to.
Let me just do this.
I'm sorry for the struggle at the time, this kind of real right now.
So we say car two miles and here I can put up this year and then we can say car two dogs get odometer
right?
So this will put ice probably for a space after this, so let's actually put a couple of spaces there.
Yeah, there we go.
And so we'll have the car, one miles will for now and we're doing car to get odometer right and then
the second one would do a cartoon odometer and it will show us the values, default values of the second
car so we can make as many objects as we want.
Let's go ahead and just print this out to see what it looks like.
All right.
So we see a car one starts out with 40000 miles as expected and car to start out with 10000 miles,
so you notice that we initialized it here.
It knows which object has which data, right?
We explicitly said Car two is an object that has this data that is held somewhere in memory.
This object, when we refer to get odometer of car to it, is different, then referring to get odometer
of the original car object that we made.
Right.
So you can make different objects now, and they can, of course, have different data associated with
them, right?
One is a Ford Fiesta with 10000 miles.
The other one is a blue Subaru Outback.
That's 40000 miles that later gets painted to black.
OK, so I think I'm going to stop here for this lesson, I'm not trying to overload too much.
We have a lot to go over with objects and classes.
There's just a ton of stuff you can do with object oriented programming.
Right now, the focus is just trying to get you comfortable with making classes.
So making the blueprint, understanding what public and private are the difference between them being
able to implement all of the prototypes that you put it here in the blueprint and the implementation
goes here?
Right?
These are all the implementations understanding you have to use this.
The class name Colin Colin before the member functions.
And then, of course, now after this lecture, I expect you to understand what a constructor is, how
to declare a constructor here in the public function section and how to implement a constructor here
down past the class blueprint where and also not only know how to do it with empty parentheses here,
but have parameters and know how to initialize all of your member variables.
The private number of variables to either the parameters that have arguments being passed or just initialize
them like feel here to some default value that was decided by you as the software engineer.
Also, of course, knowing how to instantiate everything and knowing how to call methods on it, you
know, it's just like string se length.
Remember, like I said, string is a class just like car.
Somebody else made class in this word right here was string and they had all kinds of functions.
One of these functions is what is a length.
So the name of it is length.
And they made an implementation that said, String Colin, call in length, you know, and it's going
to tell you how it's going to give you back the length of the string.
So pretty cool, right?
So with that, I think I'm going to stop here and I will see you in the next lesson where we continue
on with more of these object oriented programming practices and concepts.