So welcome back to another lesson, everybody in this lesson today, we're going to be continuing on
with our discussion about overloading operators and C++.
We left off talking about the assignment operator.
Now we are going to move on to some other operators specifically in this lecture.
We are going to talk about some mathematical operators.
So binary mathematic operators like plus minus, you know, when you have something like something else,
something minus something else, multiply stuff like that.
So I'm really just going to go over the code for the plus or addition operator, overloaded operator.
And then we'll just kind of talk about how it would be easy to transition into doing the subtraction
operator or the multiplication operator where we're rolling forward with our car class just so we can
add functionality.
And so it'll be kind of fresh for you because we've been using the car class and I think you're kind
of putting together all the pieces in your head, how all these files work together, how all this code
works together, and we're kind of building on that.
I think switching to a new kind of topic, some abstraction or new ADT would be a little just confusing,
pointlessly confusing, you know, so let's go ahead and get started.
So what we're going to talk about is this plus operators.
So I have some code here and mean CP that's commented out.
And what it's really doing is it's it's it's adding some upgrades, it's showing some upgrades.
So this is actually a new function that I added and I'll show you that in a second.
It just shows you what upgrades the car has.
And then what we're going to do is something like this where we're going to add a car and another car
is going to have a result and we're going to save that result in an existing car in this example.
So here is the assignment operator being used, and we've already handled that.
But right here is we'll need to overload the plus operator because C++ does not know by default how
to add to objects here that you have defined, right?
It's user defined object.
It's a car.
It's not a standard data type of the language.
So that's why we're going to have to overload the operator for this segment right here.
And we're going to need to return the result of a car being added to a car, which will be another car.
So we're going to have to decide what that really means when you add cars together, like what does
that?
What does that mean?
It's this really makes sense right off the bat, right?
Because, you know, you add numbers together, that kind of makes sense.
But like, what would a car plus a car be?
So we'll discuss that.
I'm actually going to go over a really like kind of basic interpretation.
There's probably better ways you could add cars together, but for simplicity's sake, I'm going to
make a very simple addition.
Operator.
Very simple.
Plus operator here.
So let's hop over to Card CP.
So what I've done is I have this show upgrades function.
It's void return type.
All it does is it goes through the.
Vector here, so this is actually a kind of an item base loop here that's going over all of the items
in upgrade code.
So I is the actual item.
It's using this as kind of some slightly more modern syntax, but I'm using the auto keyword to detect
the type of AI and then what that's going to be.
This colon is basically saying that is going to take the form of each of the things inside upgrade codes.
And you notice that I'm d referencing a great codes because upgrade codes is in fact a pointer.
Pointed to a vector.
So then it just prints each one of them out that way you can see the upgrade occurs and kind of see
how it changed.
If we add things and then we add the cars together, we want to see the before and after to make sure
that our function did what was expected.
And you can actually go over that in the debugger as well.
I actually did set up my voice code to have a graphical debugger now and have a compiler, so I might
be going over that in some future lectures on how to set that up.
But you can essentially have a graphical debugger like sea lion.
It's definitely not as good as sea lion, just kind of out of the box.
The sea lion debugger seems to be have a lot more features and tools and be more user friendly.
I'm sure some people would argue me about that.
The code has just as good a debugger, but so far it seems like the sea lion one was a bit nicer.
As far as the graphical debugger, anyways, that's a tangential kind of topic.
So let's get back to talking about our Plus operator.
So we're going to do is put it right here in the CBP file.
But first, we're going to go ahead and define a prototype that's in the public section of our header
file up here.
So since we're adding a car plus another car is going to turn returning some result car.
So I'm going to say the return type is car.
I don't need to return it by reference.
I'm actually just going to skip this ampersand and just put operator here.
So I'm going to say because it's going to be like a new car, I don't I might not necessarily make a
new card type, but I'm going to return just a car type, not something by reference where here we needed
it, by reference because we are assigning things to the this object, right?
The object that is being called upon.
We'll see how that comes into play in this plus upgrade as well.
So this is going to be operator plus instead of an equal sign.
And then what I'm going to do here is I'm just going to say I can make this contest.
I can say cost, car and object.
So it's going to be similar to the parameters of this.
Assignment operator, this is all we need for the prototype, and then I'm going to head over to the
implementation file here, and we're going to get started.
So remember return type as car and then I need to put scope resolution for the car class.
And then what I want to do now is add my plus operator so I can score their interests have and then
we need to put our concept car.
And this is going to be passed by reference because we want to pass the actual object.
So this is like the one that we're going to be adding, like right hand side, left hand side.
And so what I'm going to do now, this is going to be a really simple plus operator.
So normally it'll probably be better to maybe like.
Go through all of the upgrade codes of the object in the parameter and this object, you know, that's
being called upon.
So that would just be, you know, like if we're talking about upgrades, codes, there's the object,
right?
There's its upgrade codes, and then there's this upgrade codes, something like that, you know, which
is just basically the same as saying, OK, codes.
So what would probably be the best bus operator is to, like, make a union of those two vectors and
by union, you know, we're talking about like, you know, if anything doesn't exist in the other,
then we'll go ahead and add it to this like new vector or something, right?
So we want like a combination of all of the unique elements and both.
So we're not going to have any repeated elements.
But like, let's say one vector has the upgrade codes like one and two, and the other one has one,
three and four.
So we have one and two and one, three and four that the union of those would be like one two three
four.
Right.
It's not going to be like one one two three four.
Just because there's a one in and one of them and a one in the other one.
It's just like all of those non repeating elements kind of clumped together so that that would be like
an ideal place operator, but for simplicity's sake, I'm just going to do.
A plus operator where we literally just see which car has more upgrade codes, so just like this object,
have more upgrade codes.
Or does OBJ have more upgrade codes and we're just going to return whatever car has more upgrade codes,
so this can be someone like.
You know, having some type of car and they're like, Oh, this, you know, this guy's car has more
stuff like, I want that car, so this is like not necessarily the most sensical plus operator, but
we can write whatever code we want in here.
So it's kind of almost like a greater than operator, but it doesn't just say that it's greater than
another car and say true or false.
It really just gives like the bigger list of the car that has a bigger list of upgrades back.
But one thing that I want this to be contingent upon is that the model and the brand are the same.
So like if someone you know, if we have a Ford Fiesta and we're like, Oh, Ford Fiesta plus Lamborghini,
you know, and the Lamborghini has more upgrades, then we just get a Lamborghini.
Suddenly, you know, that doesn't really make sense.
So we're going to only do this if the car is.
It's the model and make of this car that we're adding to, the left hand side is the same as that this
object.
So let's go ahead and do that.
So I went for a conditional check here and I'm going to say if.
Model, so this is basically similar to this Arrow model.
The call called upon object, so I'm going to say if a model is equal to object model and brand is equal
to object brand, then we're good to go.
We can go ahead and try and figure out which one has more stuff.
So what we will say is we'll say, OK, well, if the
upgrade codes aero size because remember, upgrade codes is a pointer.
If that is greater than or equal to the.
Let's see, so if that is greater than or equal to the
object.
Sorry.
Know, you know, so if the objects of Great Code's size right, then we want to return that bigger
one, right, which is the upgrade code size.
So I don't even need these brackets.
You can actually augment those if you only have one line.
So what I'm going to do is say, just return the reference this for this object, right?
So if that's the case, then we're just going to return this.
So.
Then what we're going to do, I guess, is otherwise we will return the money or return the object.
So, in fact, you know, I guess we could kind of change this around because we're just going to return
this if the.
Object, if if this one is not bigger, because if it's if it's not, if this is not true right here.
They're not the same model like you're trying to add something else to the called upon object, like
the left hand side.
We're just going to return the car we already have.
So what we should do is we actually just add to this, I don't even need to put this in here.
We can basically take this and kind of flip it around like we could say that this one is a less than
or equal to which I really should like.
I think it's more sensible to put this on the other side.
So I'll copy that.
And then we'll just kind of put that here, and we'll just say that this is greater than or equal to
it, right?
If the object is greater than or equal to it, then we can return the object.
But I can literally put this all kind of inside of here and just have one big, long thing, so I can
say so that, you know, the models, the saying the brand is the same and the object is bigger, then
we'll take that bigger object like otherwise we're not going to.
So I can put that whole thing there and then all then I can have just one if and if that's the case
and then censuses is only have one line here, you don't even need these brackets.
I can just in and I can just say return object.
Otherwise, we're just going to return our same car.
So.
What we're seeing here is we're saying, OK, well, if the model, if the model of both of the car
is being added together the same and the brand of both cars being added together are the same.
And.
The thing that we're trying to add to the left hand side is greater has a greater upgrade, codes like
size, there's more upgrades in that.
Then we're going to go ahead and return the object that we're adding because it has more upgrades and
we want that.
Like if if we have a Ford Fiesta that has more upgrades, then we want that one, right?
Otherwise we're just going to give back.
The same thing, so we're going to end up having the same car and we're not going to do an upgrade,
basically.
So, you know, not like the most cynical plus operator, but the thing is, is that you could put whatever
you want in here, you could and you could go through and make the union of those two vectors.
I'm just not going to do that because I don't want to make something that's too missing detracts from
that.
You know, when we have like the foreigners and all that stuff, I just want to focus on the operator.
You know, or you could just leave it like this, or you could just think of and let's see, you can
make like.
Maybe you just make some new upgrade thing where you like kind of.
Morph the upgrade codes together and come up with new upgrades, I don't know it can really be whatever
you want.
The point is that you are the one deciding what the behavior of this operator is now.
And what's cool is that you can now use plus minus times, you know, which is multiply.
So I mean, to decide what happens with your objects, right, you're able to use simple operators now.
You don't have to have a function that says, like, you know, like car or car, find which car is
like better upgrade and return.
You know, I'm being kind of ridiculous with like that function.
But you'd have to like, make your own function that did this and then you take the concept car object
and then like, you'd have to go to Maine and you have to call that function here, right?
And pass this other car like, you know, pass one car to it and then decide and then it would return
it and then you could save it.
You'd have to be like car to dot that function pass car four.
You don't have to do all that.
You can just literally say, like, Oh, this is the behavior I want when someone tries to add these
cars that I created together.
Right?
So that's what makes it really powerful.
Over the operators makes it nice for the end user on the client side.
Like make using your library, lets see you create some kind of code library.
Someone can import it up here like, Oh, I'm going to import car that age and then now I get to add
cars together.
Cool.
So that's why it's pretty powerful, and that's the point of us learning about this stuff.
So.
Yeah, I think that should be fine.
So, yeah, I'm just going to leave it at this.
You know, we could also make some sort of.
Minus operator, you know, I might as well make that since this kind of simple like we could make something
that's flipped where it just gives us back the smaller car.
So let's go over here and do that as well.
I was only going to do the class operator, but we can do minus two.
You then it's pretty much the same.
We'll cost car and object in the header file.
We'll go over here and we'll just put this car car operator and no operator minus
cool.
And so we'll pretty much have the same exact thing.
So I'm just going to copy paste this and then we can just flip it around so that we still want it to
be the same model and make probably modern brands sorry.
But what we want is to see if it's less than right, if it's less than.
Then maybe we will.
And this one, we should probably just say greater than right now equal to you, because if it's equal
to, then we will just, uh, return the same car because we have no interest if it has the same amount
of upgrades, right?
So we'll say this is the less that in this is greater than it's pretty much the same thing.
So now we can subtract them and we'll get the kind of worst car.
I guess we get the smaller amount of upgrades from it.
Maybe we want like a cheaper car, right?
We don't want so many, so many upgrades.
So, you know, because maybe like this would could hypothetically increase the price or something.
If you have more upgrades, which we're not doing that right now, you know, if we go up here, we
don't really have a price here, but we could change that if we wanted to.
Cool.
So let's go ahead and test this out.
So I might actually do a little minus two, so we'll say showing car for upgrades below and then maybe
what we'll do is we'll say, you know, car for equals.
Let's see.
Uh, let's just say, I guess we'd have to make kind of a new a new car so maybe I can make like a car
five or something.
So car.
Car five.
Let's just say car car five, and we'll just say this is also white Ford Fiesta.
So why don't I put that here, and we'll just say, we'll say it's a white Ford Fiesta, but actually
we can just, you know, it can just be the same make and model.
So we can just say this is blue Ford Fiesta with a hundred miles or something.
So now what I could do is I could say, you know, I could say car five equals.
Let's say, let's add some upgrades to, let's say, car five and upgrade and we'll just say like five
or something like that.
And then we'll say car five equals.
We'll say car four equals.
Car five or say, like car Ford minus car five.
Something like that, and then we'll just say.
I can say this again, I'll just copy this, so I'll just say car showing car for upgrades below, and
then we'll just say.
Car for Dodge show upgrades.
This will show other upgrades there, so I can go ahead and comment this now, so.
We have this section here where I kind of like add a show upgrades for card for just to show you what
the default is from it.
Being set to car to car two doesn't have any upgrades, so it should just have one, right because they
come by default with one upgrade push back one, right?
So when you make a new car, it just pushes back one for the constructors here.
So it'll have one when we show the upgrade, then we're going to add two and four two car two.
Then we'll show cartoons upgrade and should say one, two and four.
Then what we'll do is we'll say we'll do this.
Plus we'll say Car four is equal to car two plus car for it.
And we know what's going to happen is since Car two has more.
Upgrades now it has more upgrades than car for it's going to return back to us, car to you and we're
going to say car four is now going to be having the same upgrades as Car four, so we're going to make
it a new car.
It's not just going to be the same upgrades, it's just going to totally replace with this car.
You know, it's going to take everything about cartoon put in here because of our assignment operator
so that once will show the upgrades and CAR four should have one, two and four for the upgrades.
We'll make this car fire that has a grade five, and then what we'll do is we'll subtract it and we
should have car fire then get stored in car for because it's less right.
So let's go ahead and see if that is how it behaves.
So I'm going to save these files I could use to save all, actually.
So.
I mean, see, I don't think this has.
Oh yeah, that has an error right there.
So let's go ahead and save that too.
So I'm going to go ahead and.
Compile this, so I think it yeah, it doesn't like the unsigned injury comparison to when I was compiling
it before I.
I think that's not something that we need to worry about now.
So I'm just going to do I'm actually just going to compile and here I'm going to do G Plus Plus and
just do star.
So all of that seeps because we want to compile co-recipient.
Mean may not be.
And when I say that show, and I'm just going to call this mean that you see.
OK, so.
OK, so I'm returning the cons, Jeffrey, and so let's go ahead and make this.
Let's go ahead and make the speedy.
Owners can change to non-conscious now, we can also make this return a cost.
But where am I at?
Here we go.
So this is a common story, and so we're returning to here, even though we say that we don't return
to, so we just do cars, so there could still be problems with this in Maine if I'm returning like
a constant to it because ideally what we do is make a new one.
So why don't we just make like A.?
We can make a new car, right?
So we'll keep it.
Cons But what I'll say is I'll just say I'll make I'll do a smart point.
And what if we do that with same type car smart pointer?
And I'll just say new car and it'll be a new type of car and we will just say that.
It'll be we'll use the assignment operator, how about that?
So I'm going to put this here.
You put some brackets together, multiple lives now.
And what I will do is I will just say that the new car so I'll do reference this and I'll say new car
equals.
Uh, oh, actually, you know what I can do, I can just make this.
My to use the copy constructor, except it's going to be.
Let's see.
Yeah, that should work fine as a concert.
Assured finest comments, so I have concert in there, so I can actually I should be able to do this,
I think I should say new car equals object.
So hopefully this will work.
So let me see if I can copy this over this code over here, so I'm just how feeling like all this?
Put it here and then of course, I'll just go and I'll flip the symbol, right?
Flip the symbol there, and hopefully this will work fine.
You know, the smart pointer here, well, of course, delegates also, I'd like to use that and then.
We'll return these, right, so I'll just we'll just reference this and return new car.
So let's see if we can get this to work.
Don't know if it will, but.
OK, so.
And valid use of this in non-member function.
One 18.
Seen declared a prayer here.
Brand was not declared in this.
Sculpted, I have the scope resolution are there.
This was lowercase.
Sorry about that from there.
Let's see if that works hard to Cassius employers.
OK, so that seems to compile fine, sorry for the wait there.
It's hard for me visually to see that I mean this lower case, so scope resolution was.
Not resolved, and it was saying like, what are these things?
It says you don't have access to this, you don't have access to this and this, you know, so yeah,
now we should begin to go.
So let's go ahead and run this.
So let's see what it says here.
So showing car upgrades below, so here let's go ahead and look at main.
So we were saying.
This should have won for Australia, so it shows one, then we add two and four to car two and we show
cartoons upgrades and it should have one, two and four, which it shows.
And then let's see.
When those objects go out of scope and the local functions, it calls the destructor, so those are
the.
Smart pointer.
And then let's see, we do.
So, yeah, we all showed up, guys, here's the car two plus car four, so we're going to go in there
and show a car for upgrades below.
So we have one two four.
Right?
So a car for was this.
Now it's having all of these upgrades because we chose the one that was bigger, right?
So that's why right here is making this print out, these instructors are, I believe, because of the
smart pointers going out of scope here to these local smart pointers that when they go out of scope,
they are.
Cleaned up, so, uh.
Now we add upgrade to car five.
Then we do a car for is now a car for minus car five.
And so five should have one in five, right, and we noticed that when we now say showing car four of
grades below again, it's now has just one and five, right?
Because Car five had less upgrades than car for.
OK, so I think that that is all that we're going to go over in this session.
I'll talk more about the mathematical operators at some point.
But pretty much they're all the same.
We can decide the behavior for each one.
You noticed that we were able to copy over everything from the plus to the minus, and you could do
the same thing with the multiplication operator.
You would just decide how you want the behavior of that to be as far as the code locally inside this
function.
All right.
So with that, I will see you in the next lesson.