OK, so welcome back to another lesson for object oriented programming.
So in this lesson, we are going to talk about a few things you won't be as difficult as a lecture as
previous ones.
I'm not going to stuff as much information into it as previous lectures.
So what we are going to go over is how to set up a kind of that object oriented programming multiple
file situation here in Sea Lion.
And then I'm going to.
Show you a new concept called a decent destructor.
It is similar to a constructor, but kind of the opposite thing.
So rather than constructing an object, it will destroy an object and then we will finish up by looking
at some pointers to objects just like we had pointers districts and we'll do dynamically allocated objects.
So let's go ahead and get started.
So right here, I just have made I've made a new project in here.
I called it object oriented programming pointers to a folder.
So I'm going to right click this folder and I'm going to say new and you notice that all this stuff
pops up.
Well, I'm actually going to say is, I'm going to say a C slash C++ header file, and I'm actually
just going to call this car because I'm going to use the same car class.
So I'm going to say a car.
You can just leave this checked ad to targets.
It doesn't really matter the targets as far as the compilation process, and we don't really need to
compile the headers, but I'm just going to let it be added to the targets because it won't cause any
problems right now.
So I go ahead and add this, do you notice it nicely puts these macro guards here for us?
So we get the macro guides here, and that even puts a little common here on that.
And if so, all I do is copy paste some code here.
So let me just go ahead and grab some code.
I'm getting it from the window and I'm going to pay you the car class.
So this is the definition.
And then I'm actually going to show you how you can make some.
A cool air and sea lion to deal with the implementation, so I'm actually getting it right, click this
and say new, and this time I'm going to say C++ source file.
So I will say, see slash C++ source file.
We also could have done both of these by doing the C++ class, but I just am doing them separate.
So this is going to be the class would have handled both of these at the same time, but now I'm actually
saying explicitly, I'm going to make another source file, which this deep file.
And then so what I can do is go in here and if I.
If I do this, it can say generate definition for constructor car.
So you notice I click that and what it does is it automatically created the constructor inside of this
car that CBP file.
It also went ahead and included Carnot CP for us, which is pretty cool.
So we can do that for actually all of these, so I can just hover over this, I can say generally definition
for that.
Go back here, I could say generate definition for this and you can just keep going and it does all
this code for you.
It sets up the member function implementations for you in this implementation file card.
So pretty nice thing for sure.
Yeah, but you replace this.
But yeah, I can go ahead and I'm going to finish just copying the rest because I don't really want
to have to deal deal with all that, so I'm going to go ahead and grab it from afar right now.
This is just a little too rough for me to type right now, so that's why I'm going to just kind of replace
all this stuff here.
Copy and paste, OK, so copy paste in all the stuff that we had previously in the implementation file
of our car class.
And then so now what I want to do is talk about this first new topic, which is the destructor.
So the destructor is something that's very similar to the constructor, but it kind of cleans up the
object in memory.
So here is where we initialize that object, right?
So what we're going to do to make it destructor is have something that looks a lot like a constructor,
but the only difference is that it's going to have a tilde in front of it.
So I'm going to say, tell the car and this one, I don't need to put anything inside of.
These parentheses here, because I'm not it's not like we're passing explicit things to it.
You know, you can also have a default constructor here, too.
You notice that this right here is like a parameter based constructor.
So we have our constructor accepting a bunch of parameters that the user sets, but we do not have like
a basic variable default constructor, so we could put that as well.
And you notice that highlights.
So, you know, you could generate a definition for this, but C++.
Well, go ahead and just have a default constructor for you as well, kind of automatically so you don't
have to do that, but we do want to generate destructor just to have it in there.
So I'll go ahead and click that.
And now it makes a nice destructor for us here.
In our implementation file.
And so the thing about the destructor is it's kind of just called automatically when objects go out
of scope and when I say that, I mean, like stack allocated objects, not dynamically allocated objects.
So let's go ahead and make an example here.
So I'm just going to put some code in here.
Actually, I think I'll need to include
extreme.
And then one of my do is down here, I'm just going to print out a little message here, so I'm just
going to say see out and inside the structure, cleaning up object.
Cool.
So I'm going to have to do this, so I'm not using namespace nastily, so Anderson here, just so you
can see when the destructor is getting cold.
So let's head over to main and I'm actually just going to copy paste some code here in Maine as well
that we have before.
So this is just making our car and car two objects, all that good stuff.
So go ahead and paste that code here.
I'm actually going to need to include a card h.
So let me go ahead and paste this in here.
So now I make a couple cars and we print out all of the information and stuff that we were before.
Let's go ahead and try and compile and run this to see what prints out, and then we can see where the
destructor is actually being called.
So, you know, here is our here's our destructor.
So we will see whenever we see this message print out.
We know that in comparison to win this other stuff prints out, we can tell that once the objects are
kind of out of scope, which means they're out of Maine's stack frame, then the structure will be called
to clean that up.
And just in a moment, we will be looking at a dynamically allocated objects using pointers and stuff,
and we'll notice that the destructor can actually help us clean that up to accept that we have to explicitly
delete dynamically allocated memory, remember?
So let's go ahead and run this and see what we get.
Cool, so you notice here that it printed out twice at the end of everything.
So I'm going to try.
Oh, cool.
I can zoom in on the output as well, so hopefully that's easier for you to see.
So this is all the standard stuff that we've seen print out before, right?
We got these car miles like the initialized miles here that were sent to that constructor to add parameters.
And then we see 12 blue, three seven black.
This is all just from this code right here that we've seen before.
It makes sense that it's printed out.
You know, this is the last card I get Andy Card.
I get paint right here on these two lines.
And then once we leave the scope here, we notice that the destructor is called not once, but twice.
And why is it called twice?
Well, it's called for each object, and we made two objects.
You can think of this class.
Each object is a instance of all of this, you know, all of this file and all of this file.
If you can think of it as a copy of this, like we have, you know, car is a copy of all of this and
all of this together, and then Car two is a copy of all of this file and all of this file together.
And so that's why when we made those two objects in Maine, once we leave the scope of Maine, it's
time to clean up the destructing.
It's called not once, but twice because we have two objects.
So that's kind of just what the destructor does.
It's not, you know.
Incredibly interesting right now, maybe, but it is very important because we don't want memory leaks,
right?
We talked about memory leaks.
With not cleaning up dynamically allocated objects, and, you know, this is something that is important
just to clean up the object and its most common use case is that it will be called to clean up objects.
If that is the case, something that you'll see later on is that if we have a class, there might be
more code in here if we have a class that, let's say,
has dynamically allocated.
Members or something like that, one need to clean those up, like if we have some sort of container
or data structure, something like that that is dynamically allocated and that is one of the data members,
the like, you know, private or public, but most likely private data members.
We all have to handle deleting that in this destructor.
So we might have to like, iterate over certain containers that contain dynamically allocated objects
and, you know, get rid of all the stuff in there.
So if we had some sort of let's say we have like a vector.
Of this class that was like right here, we had some vector in it, held a bunch of dynamically allocated
memory like pointers to dynamically allocated memory.
You might want to handle cleaning that up in the destructor of the class.
So just kind of, you know, a little foreshadow of some stuff we might need to do in the future of
this course.
Most likely more towards the data structures and algorithms portion.
But you never know we might encounter it in this first half of the course as well.
So that shows where the instructors call, let's look at making some dynamically allocated objects here,
so I'm going back to Maine, so I can say, I'm just say car.
Same car cleaner car equals new car.
So what I'm doing right now is I'm using this constructor.
That has the perimeter as a surprise constructor.
It's the one that takes parameters, not the default constructor, but.
What I'm doing here, let me explain this line, so this is the data type right here.
This is saying that the type of this data is car.
This right here is, of course, saying, OK, well, it's going to be a pointer.
You know, this is really actually going to be a pointer to a card data type.
And the variable of this point is going to be called car.
And then right here on the other side of the equals where of course, using the new keyword to dynamically
allocate memory and create a new car object.
So kind of similar to how he did structs, but what we're doing right now is we're doing we're using
the parameter rise constructor of car, which is, you know, in the header.
It is this.
And in the implementation, of course it is this at the top.
This is what we're calling when we're making this dynamically allocated object and we're creating a
pointer to it.
Of course, you notice these all turn red right now, and that's because we need to use an arrow, an
arrow.
You can see that it suggests that we can use the arrow so we can go ahead and click that, and it will
change it automatically for us here as well so I can go through, hover and click.
We're over this and click and just do that and all of this, which is kind of nice, honestly, because
my wrist is a little bit hurt, so.
And then you noticed that these are all just for car, but of course, we have car too is not being
asked to be changed because we have not changed that to a dynamically allocated object.
So since we dynamically allocate this, we're going to need to handle its deletion.
So right now, we would have a memory leak.
Let's go ahead and run the program.
You notice that it only calls the destructor once now, and that's because the destructor got automatically
called when this was a stack allocated object, which is like car to car to is the one that is responsible
for this printing out right here, the destructor clean of the object.
But since we have not explicitly called the delete key word on this dynamically allocated object, it's
not deleting it and therefore it becomes a memory leak.
So that's not good.
So let's go ahead and handle the deletion of this.
So down here I will say if you delete car and let's see what happens when I do that.
So now you notice that, in fact, cause the destructor twice because it knows we wanted to delete that
car object, it deletes not only car two this time, but it also first deletes car the original car,
not car one, but this right here, which we had a point or two.
So I'm just kind of kind of leave it at that.
I think in the next lecture, I'm going to go over kind of an interesting concept.
I wasn't going to do this at this point in time, but I think that it might be good to go over.
Type of pointer that cleans itself up where you don't have to use the delete key word, so I might be
going over that because I think that could be kind of useful, although I might not always use it in
the in the course.
But it is a really good thing to know because in modern C++, a lot of people are trying to use these
things called smart pointers, where it's a little bit safer and you don't have to delete that, it'll
clean up itself.
So with that, I am going to finish up this lecture and I will see you in the next lecture.