OK, so now we are going to be transitioning into a new sub topic.
This is kind of related to the copy constructor, but what we're going to be looking at now is something
called overloading operators.
So you can kind of see these down here.
These are operators that you are familiar with.
We are going to be making special functions, member functions of the classes that can change the way
that these work with our objects.
So think about operations on abstract data types.
When you're working with numbers, for example, everything is pretty straightforward, right?
So if you have something like a equals five, then you say a equals seven.
Now, a holds the value seven instead of five, right?
Or maybe you say a equals A-plus five after this, and now you would have 10 stored in the variable
a right.
What if you wanted to do this same thing with objects, though?
So you wanted to be able to add objects together or assign objects to other objects or subtract them
or multiply them?
This is what we're going to be going over in this section.
So to use these operators with abstract data types rather than the standard ones like, you know, numbers,
strings, stuff like that.
We will need to create our own member functions of the class that define how to perform these operations
on the custom objects that we're making, like the car class, for example, which we will be looking
at right after this.
So this is what we're going to discuss in the next few lectures.
We're going to go over.
I'm going to kind of split it up so we can separately look at each one of these operators.
In this lecture, we are going to focus on just one of them, which is the assignment operator.
So the assignment operator is very similar, like as far as overloading, it is very similar to what
we've already done with the copy constructor.
So by default, assigning an object to another object is going to have the same effect as what we noticed
with the default copy constructor.
If we do not make our own overloaded version of the assignment operator, then we will get a shallow
version of the object.
So think back to the copy constructor and how we talked about that shallow copy that wasn't really good
when we were using dynamic memory, right?
So it just made a copy of the pointer rather than allocating new memory for that vector.
So if you don't remember, you could jump back and look at it, but we're about to look at it again,
so it'll kind of refresh your memory.
So we're going to run into the same issue when we're assigning an object to another object.
If they have a member variable as like a data member variable of the class, that is a pointer.
Then when those variables kind of get copied over to deal with this equality, they're going to actually
use the same pointer for two different object.
You'll have the same pointer, so those variables will point to the same memory.
So we have to come up with a custom way of assignment.
So let's go ahead and hand over to the editor to discuss the syntax for this.
So I'm going into sea line here.
Hopefully, it's big enough for you to see everything OK.
So here you notice that I'm in the header file card, not so familiar project we're looking at, you
know, not real project, but the code we've looked at before for the car class, right?
So here is our copy constructor that we made.
And we're going to be adding our overloaded assignment operator into this header file and the CBP file.
But first, let's go ahead and jump over to me and take a look at something.
So what?
I have a comment, an outline right here, and what I'm going to do is show you kind of just like I
did with the copy constructor, what happens when we just use the default assignment operator for objects?
So I'm going to uncomment this.
And so you see what it's doing here.
As you know, this was the line that we actually use for the copy constructor, right?
And this is using the copy constructor, this highlighted line right here directly after it.
What I say is I say, OK, well, actually, I want Carrefour to be the same as car to.
And then you notice that I change car for a change, I add something to change something to the object.
I add an upgrade, right?
So think back to the add upgrade.
Let's take a look at that.
What is that?
Add upgrade?
Well, remember that it's a pointer to upgrade codes, right?
So this is a vector.
So we have this upgrade codes.
And when we add upgrade with this function right here, let's go into the CBP file and take a look at
that.
So add upgrades.
What it does is it just pushes back a code to the upgrade codes.
You notice like, let's take a look at this copy constructor here.
So this is dynamically allocated.
We have this upgrade codes here, so in the copy constructor, we're doing a new vector for upgrade
codes and in the normal constructor, both right here and right here, we're also making dynamically
allocated memory for this vector.
So this is going to result in the same kind of issue.
It's going this upgrade codes.
So let's go to here.
This upgrade codes data member if we assign it to a.
Another variable here like Car four is equal to car to this upgrade codes, vector is going to be the
same region of memory for both of these objects.
It's just going to copy over the pointer address.
So this will have the car for will have the same vector codes as Car two and we don't want that.
We want both to have their own vector codes, so we're going to need to handle that.
But I'll go ahead and show you what happens with that.
So I'm going to leave this breakpoint for the debugging here.
I mean, zoom in more where we can see it's very well.
The other one was zoomed in, but not this one.
So I'm going to go ahead and run the debugger now, and I'm going to show you kind of what happens here.
So we'll step to this line and step to this line, then step over and we'll see what the ad upgrade
to did to car for and car to.
So let's go ahead and debug this.
So let me bring this up here, so we stopped at this line right here.
I'm going to go ahead and step over.
So Carrefour has been created and it's is set to part two.
This is kind of, you know, so I have car two here.
I could say, let me change this just to Manhattan.
You know, it seemed kind of redundant.
So I'm actually going to say.
Let's see.
Maybe I could just say car.
Like the reference it or something like that, so we can say car four equals car and then we change
it to car two, that makes a little more sense.
So let's go ahead and do that.
So I debug it.
So let's go ahead and step two here.
So now we have a car for and it should be the same as car.
So we got this blue Subaru Outback.
Let's check this out.
This is a blue Subaru Outback with 40000 miles as well, so it's the same same thing copied over upgrade
codes this one.
Let's check this upgrade codes is one.
So Car four is pretty much the same thing as car.
So then let's go here and I'm going to step down.
And now we've gone over this line.
So now Car four should be equal to car two.
So let's go ahead and check that out with his car, too.
Well, it's a white Ford Fiesta with 10000 miles now.
Car four is a white Ford Fiesta with 10000 miles, so it looks like it's copied everything over his
assigned car to the car.
Four.
Let's take a look at the upgrade codes.
One.
For an upgrade code OK, that's good in car two.
That should be one, right?
So this is one for this as well.
Everything looks fine, right?
We've copied car two over the car for, well, let's see what happens after I step past this ad upgrade
and we add the upgrade of two to car for.
So I'm going to go ahead and step over.
So.
Let's check out car for first.
So I go to upgrade codes.
Well, that's what we expect.
Added to car for an upgrade code of two.
Everything else looks the same as it was before, so everything seems fine.
Car two shouldn't have been changed, right?
Because we just wanted to add a upgrade to car four.
Let's go ahead and see what car to says.
So if I go over to car to, oh well, look, it actually added upgrade code two to car two.
And that's a problem, right?
And why is that happening?
Well, it's just like I said, it's because Car Foreign Car two are actually sharing the same upgrade
codes because it's the same region of memory.
All it did was copy of the copy, the pointer address over, which is pointing to the same chunk of
memory that stores the vector.
So they're using the same vector, and that is definitely a problem.
So we're going to have to change that by overloading this assignment operator, which means making something
like our copy constructor, it needs to be a special function.
So C++ knows that when it sees this and it has two objects, it's going to use whatever code is in that
function that we make.
So let's stop this debugging process.
And I'm going to go back over to Corridor H.
What I'm going to do is I'm going to add something here that will be our prototype of the overloaded
operator.
So what this is going to do is it's going to return the type.
Of the class.
So it's going to return car object, but specifically what it's going to return is the calling is basically
going to return the object that is being called upon.
So I'll explain that more later.
But if that's that's what it does is it needs to return to the car object that is being set that's been
having something set equal to.
So what I mean by that is like if we go over here, so it's basically trying to return, return this.
So we're copying everything from here into here.
And what we need is to return this object here, and that may seem confusing, but I'll show you how
that's going to work.
We're actually going to use a special keyword called this and that will represent our object that's
on the left side of the assignment.
Operator.
This one right here.
So let's go ahead and do that and what we're going to do since we're returning that actual object,
we need to return it by reference.
So I'm going to put an ampersand and then what I do is I put the word operator because we are wanting
to overload an operator, right?
You'll notice that this is going to be common amongst all overloading of the operators.
So I say we're going to overload an operator and we're going to return so this far right here just to
recap this says return a car object by reference.
This right here says we're going to overload an operator.
Which one are we going to overload this assignment operator?
Then I put the parentheses for the parameters, and it's going to be just like this right here.
I'm going to put the other object passed by reference.
So what is this object in here?
It's this one.
On this side right here.
Car two is being passed into the parentheses as an argument here.
And what we're going to do is very similar, pretty much the exact same thing as the copy constructor.
We're just going to take everything from this object and we're going to have all of our data members
like down here be set equal to.
And when I say set equal to, I really just mean a sign.
We're going to sign everything from this object over to this object.
So each one of these needs to be, I guess you could say, copied over or overwritten over onto.
The object on the left side, which is this car for, so hopefully that makes sense, that will make
more sense when we're getting into the implementation.
So this is the prototype, this is all good.
I can just click this little light bulb and I'm going to say generate definition for this function.
Cool, and I'm going to delete that, and what I'm going to do is literally just copy over this code
from the copy constructor because it's going to essentially be the same thing.
I'm going to have to modify a few things, but not much so my copy of this code.
I'm going to paste it down here.
Let's go ahead and zoom in a bit too, so we can see better.
So we have this thing.
Orbs right here is the name of the variable in the parameter, so I'm using that object to it.
It was the same as the copy constructor up here.
So that was kind of nice to name it the same thing, so it would be easy to copy over.
So the only thing we really need to change is we don't want to make a new vector.
All we're doing is changing the existing upgrade codes and all of the other variables to whatever is
stored inside this object.
So let's talk about this thing.
So first, what I'm going to do is instead of, I'm going to get rid of this, actually, so we're not
going to make a new one.
What I'm going to do instead is I'm going to clear it out.
So when I do upgrade codes like this, I'm not doing object upgrade codes, object upgrade codes, you
know, that's that has to do with this object right here.
Just upgrade codes.
Is this object right here, the car for?
So just want to really make that distinction very clear.
So let's go here.
So this this car object right here is.
Cartoon all this stuff right here like upgrade codes, model brand, paint color, those are all parts
of another object, right?
And what object are they part of?
They are part of this object, the car for us.
We refer to it as the calling object like right here.
When you say car Ford Dot ad upgrade, we're calling an ad upgrade on car four.
And when we say we're calling something on car floor, we can refer to car for as the object that's
being called upon.
And that object is represented by these data members in this class.
So when this function gets called, the thing that is on the left side of the equals sign right here
is going to be represented by all of this stuff on the left side.
So this this this, this, this this all on the left side of the equal sign over here.
The thing in the parameters is the one on the right side equals sign, and it's the same for like all
other functions.
So we have an upgrade here.
If I wanted to refer in this function to Carrefour, which is calling the ad upgrade function right
and of is being called upon car for right car for could be represented by a special word called this.
This refers to that object that's being called upon and upgraders being called on car for.
And so when I go to the code right here and add upgrade gets executed, right?
Because when this gets cold in Maine, it's going to jump here and it's going to run this function.
The code in here, if I type the word this is referring to, it's referring to the car for whatever
is on the left of the dot right.
This is the car for it's something dot and upgrade.
The thing before the dot is the object that's being referred to.
And we can specifically refer to that object as this because it's like this is the thing that's being,
you know, upgrade upgrades being called on.
It's like this invisible object, right?
We can't see it.
This object we can see, it's obvious, right?
This one's being passed in the parameters like, Oh, look, it's a car object.
We can call a dot view on it and odometer.
But if we don't have any dot fuel, if we just say fuel an odometer, there's like an invisible object
right here before right here we have an object that's visible object dot.
What about over here?
There's no something dot odometer.
There's no something dot fuel.
Well, we could actually use the word this, and it would be the same thing can say this dot fuel.
And I can say this odometer,
and this might be a problem because it needs to be a pointer, actually, so this needs to have an arrow
because of the this keyword so we can change it to an arrow.
So rather than the DOT, it'll have an arrow because this is referred to as a pointer.
So that's just the syntactical thing, but you can think of it as the same thing.
There's object fuel, which is car to.
Fuel fuel of car two.
And then there's this air of fuel, which is car for in this example, right?
So I just want to make that very clear.
That's kind of the most confusing part is you're you wondering like, OK, well, what is this stuff
over here like?
These are just the variables I don't understand.
How are you setting one object equal to another object?
Well, this is the object, the word this, but we don't have to put it.
It's kind of unnecessary because C++ knows that when you just say fuel, you're talking about this arrow
fuel.
Or this era odometer?
All right.
So let's get back to what we have to do for the rest of the code here.
So one thing we have to change is we're not going to make a new vector.
What we're going to do is clear the existing vector.
So if anything is inside of upgrade codes, we want to clear it now.
So we are going to say upgrade clothes, upgrade codes, dot clear.
And this could actually.
I think this needs to be an arrow.
So we'll see upgrade codes aero clear.
And then what we want to do is have this be.
This is going to do the same thing, so it's just going to clear is going to remove everything in the
victor, so clear it all out.
So it's back to just being empty and then we can use the same loop to add all the stuff in OBJ, which
for our example, is car to write everything and obviously will get copied over.
And it actually it's going to like get added into the upgrade codes of great codes as empty after this
line.
And then all the stuff from object is moved over into upgrade codes or at least copied over is what's
really happening.
So I said that we had to return the object being called upon, so we're going to have to return this.
So I'm going to say, return this, but I can't just return this.
You see it underlined and read it says, Oh, you're returning car, you're trying to return car pointer
because this is a pointer, right?
That's why we had to save this aero field because this is an employer.
So I'm going to have to reference this to return an actual car object, right?
I said, this operator is going to return a car object by reference.
So what I need to do now is put a star to reference this.
And what it's really doing is it's going to return in this example car for.
So go ahead and put a semicolon here, and this is all we really need, right?
The only things that we changed from the copy constructor was that we cleared out the vector instead
of making a new one.
Right, so this having a sector made a new vector because we were trying to make a new object that was
a copy of the other object, we're not trying to make a new object now.
We're trying to change an object, we're trying to change car for which is this to be exactly whatever
car to is, which is object in here.
So kind of a lot confusing syntax, but you just you remember when Lincoln want to return a car by reference
and that car is going to be key where this and you're going to have to reference it.
So it's not a pointer anymore.
And that we're just clearing out the venue this time.
So let's go back over here and see how it's different.
So we've added that in, I'm going to go ahead and debug this again, and let's see if we still have
the problem with the ad upgrade being added to Car two as well.
So I'm going to debug.
So let's go ahead and go through here and then step over this line.
Let's just confirm one car to his car.
Two has upgrade code.
Only one car floor has upgrade code only one.
They are both white Ford Fiestas.
Let's go ahead and step to the next line.
So this line will execute here.
So I step over.
Let's check car for car for.
Got to added to it.
So let's go check car two to see if it fixed it.
Notice it's not adding any more to car to.
This is because we are actually referring to a different region of memory.
So it's actually not trying to make, you know, use just copy over the pointer.
It's actually changing stuff.
So it just goes into the memory of the other car.
So it's pretty cool.
Kind of a little bit confusing the syntax for these things, but it's really just going to get easier
over time as you use it.
More often, the things that I talked to you about where the most important really, I think the concept
of understanding that this stuff on the left side is really the same as this arrow fuel and this arrow
odometer, you know, we can change it all to this.
I'll just put this before everything.
Or I can even leave some off like legality of some off here, I'll see this arrow on some and some I
want to do this and I'll go ahead and stop this and I'll go ahead and show you that, you know, I'll
make sure I save everything which it saves automatically when I'm debugging anyways.
But we'll go ahead and debug again, and I'll show you that it doesn't matter.
It'll be the same exact thing.
So let's go ahead and step over.
Car four equals car two.
It still copies everything over, so upgrade code is one and still a white Ford Fiesta.
All that stuff copying over.
So look at this white Ford Fiesta.
Let's take a look at our code again.
So we had the this arrow on fuel odometer, painkiller and brand.
So you already know that it's the same thing.
I mean, it worked because paint, color and brand are using this arrow now.
Right.
Model is not.
But you notice that they all couple copy over so brand is Ford.
For both Katya and Carr for.
And model does not use this, and it got copied over fine, too.
It's fixed on this in this vest on this too.
So this arrow is the same thing, is not included in all, but this kind of helps you understand which
object is on which side and what you're really doing, and it helps you understand how this kind of
works like it returns the object, the car for object so that it's able to copy over this stuff.
So this gets passed to it, it gets copied over and then it needs to return it so it can actually have
this new copying over car for with all the stuff coming over from car to.
Of course, so that is all I'm going to go over in this lecture.
Hopefully it was enough for you to understand the concept of just this operator with the assignment
overloaded assignment operator in the next few lectures, we're going to be continuing on kind of probably
dedicating one lecture for each operator.
Sometimes maybe I'll include two if they're very closely related because it will be easy to transfer
over.
All right.
So with that, I will see you in the next lecture.