In this lesson, we are going to talk about one of the overloaded logical operators we have already
exposed.
The assignment operator and some other mathematical operators like the plus and minus.
Of course, there are other mathematical binary ones that you can use, like the multiply and even the
divide as well.
But I'm not going over those because you kind of can realize from the plus and minus how you can set
these up.
So what we're going to do kind of a similar thing with biological operators today, I'm going to go
over the quality operator, which is not one equals sign, but two equals sign, right?
That's what you use to check if something is equal.
And just like before, we're going to be comparing objects, right, we're going to see if one car object
is equal to another car object and we are going to decide the logic for the operator for that.
Like, what does it mean to compare one car to another car for equality?
You can also overload the less than and greater than less than equal to greater than equal to.
And a very similar fashion.
So these are going to be logical operators, right?
So they're going to say either true or false, like something is either greater than or less than something
is either equal to or not equal to right.
So because of that, we are going to say that this is going to return a Boolean, so we'll say BOOL
and then I'm going to type operator error just like before, but I'm going to put two equal signs here
and then I'm going to put the same object here when passing a constant car object by reference.
And so are a quality operator is going to look like this as far as a prototype, you notice, I mean,
cargo a year, so now we've got our prototype set up.
Let's go to the CP implementation file so we can implement our operator.
So I guess some space to say here.
So I'm just going to say pool, but car from the scope resolution and I'll say operator equals equals,
we need to put our parameter right so our cars car and of and then now we can think about our implementation.
So what do you think we should put to see if a car is equal to another car?
I think that in my mind, a car would be the same as another car if it was the same model, the same
brand.
The same paint color.
And probably the same upgrade features, right?
I think that it would be fine if the car had different miles, because one thing if one of the vehicle
to another in this instance, at least this is the way I'm going to set it up is that, you know, maybe
someone has a white Subaru Outback, White Subaru Outback with, uh, let's see, like some media upgrade
or something like that, if someone else has a white suit outback with some media upgrade.
It's pretty much like the same car, even if that might have like less miles or more miles.
I think it would be safe to say, Oh, you have the same car as me, you know?
So with that in mind, let's write some code in here and to compare those things, right?
So just like the other operators, we're going to continue with the pattern of looking at the this object's
data members.
Right.
So model paint number or painkiller brand?
So here we got model right and this object's model, this object's brand, and then we have to compare
it to the parameter object and its attributes or data members.
So let's go ahead and do that.
So first off, I'm going to put an a into just kind of looks a little bit like this up here, but we're
going to compare the paint color as well.
And we're also going to compare the upgrade code size to the object that past objects of code size.
So let's go ahead and do that, so I'm going to say if the model is equal to you.
I'm Jake Scott model
and the brand is equal to object brand and we'll say the paint color.
It's also equal to the past objects, paint color and then it's getting me long hair with you.
And no one, I'm going to say.
And what I want to do now is I want to check to see if it has the same upgrades.
But what I'm going to do right off the bat is just see if they have the same size because if they don't
have that, if they don't even have the same amount of upgrade codes so they don't have the same amount
of upgrades, then they're not the same car, right?
Like if one has a media upgrade and you know, that's one item in there.
And then the other one has like a media upgrade and like seats package or something, then you know,
that has two items and the upgrades upgrade codes list.
Then you know that they're not the same just because they're not the same size one lately has one upgrade.
So that's why I can just put that in here, and we will be able to say that it's false if any of these
fail to condition.
So I will say, and we'll do upgrade codes, and since it's a point I to do with aero and the aero size
is equal to the object that upgrade codes aero size.
Cool.
So now we have everything set up.
I'm going to put some brackets here and then now I need to actually see if the upgrade codes are similar
or not.
So I'm going to make a foil loop and I'm just going to say intake or zero, and I is less than upgrade
code size.
I can choose either or right, because at this point, if it makes it inside the if, that means that
they have the same size, the upgrade codes vector has the same size for both objects.
So do I as less than upgrade toaster size so we can go through all of them and then I'll do it first.
And then what we want to do in here is what I'm going to do is if I ever see any that are dissimilar,
I'm going to immediately return false because I know that if they if you suddenly see something that
doesn't match, then that's going to be false.
This kind of would assume that they have the upgrade codes added in the same order.
You could go through and see, I guess, to see if one even exists in the other, so we could do that.
So that would be kind of like checking to see.
For each upgrade code and one, if the upgrade code existed and the other one.
So you'd have to do kind of a nested loop for that.
So we could have some upgrade code
equals
upgrade codes.
So I'll have to do reference this.
So I'll say upgrade codes, I sort of mean integer.
And then what I can do is do some other loop here.
So let's say in J equals zero.
And then I will do, I will say.
O.J. is less than and I can just do upgrade codes.
So this is really going through the other one.
So I guess just to make it seem nice, I could say object sort of gay codes exercise, but we know that
they're the same length, right?
Because if it had entered in here, they would be the same length.
So I'll do a J Plus plus and then what I want to do here is I want to see if the upgrade codes I is
inside of the wall exists inside of here at all.
So I would probably want some other Boolean here, so I might just have like bore
the same equals.
Let's just say I want probably want to say false to start it off as because I want to go through here
and if I see the matching code, I'll just set that to true.
So I'll put some f here and I'll say if the
object.
So what we're going to have to do is, do you reference this object dot upgrade codes?
So that will be referenced in parentheses.
And we'll say object of great codes, IE is equal to code, then we will say that the same equals true.
And then each time we'll probably have to reset this to false.
So after this, Lou, I'll go ahead and just say the same.
I actually, I guess, like right before we will set it to boss.
So I'll say the same people also each time we can kind of refresh that.
So make sure that they have.
Did that that upgrade code or at least exist inside there each time?
So we're going to have to go through and we're going to have to check that for each one of them, so
each one of these upgrade codes needs to be checked for.
And so basically after this, we will want to check to see if one of them wasn't contained in there.
So we could put an f here.
And we could say if
let's just say it's not the same, because that means that if it was false, then we would know that
it wasn't contained in there because it would have never gotten set to true.
And so if we can say, if not the same, then we could immediately just return false from this function,
because that means that we never found the upgrade code, right.
We're looking at each one of these upgrade codes and trying to see if it exists in here.
So we would return false if something was ever not the same, right?
So that means it didn't get set to truly reset it back up here.
If we're able to successfully go through all of these, then we get rid of the space here.
Then that means that we hadn't returned false at any point, and that means that all of the stuff is
the same.
All of these are the same, and we notice all the upgrade codes and one of them exist in the other.
So at this point, I can just return true.
Then we need to add one last return down here because if it never, if it didn't return true here,
let's say this.
If right here evaluated to foster one of these was not matching, then we're going to return false down
here.
Cool.
So that should be good to go.
So I'm going to make some space here.
All right.
So I'm going to go ahead and save this, and I go ahead and say this, and then let's go over to Maine
and let's add a little check.
So this is how we're going to use it and the client side code.
So what I'm going to do right here is just compare car to and car for to see if they're the same income.
Then we can compare car five and car four because affect different right car five has an upgraded five.
So we know it's not going to be the same as Ford because Ford never got that upgrade five added to it.
But Cartoon Car four should be the same because car for here.
Did car plus car for and what actually happened there was that remember we have that plus operator that
returns like the better one that has more upgrades and CAR two had better upgrades.
So car to essentially car for essentially became car two after this line right here.
So those should be the same so we can put something right here.
And we could say if car for equals equals car two, then we can print out car or or, you know, we'll
just say car four and car to the same
cool.
So let's go ahead and test this out.
So I'm going to go ahead and save this.
Hopefully don't get any errors.
I'm just going to do C++ start CBP.
So it's both main and courtesy.
We get compiled and I'm just going to overwrite this man that he exceeded here.
So I'll say, and you see.
So great looks like it compiled with no problems.
Let's go ahead and run this easy.
So let's go ahead and see what the results are.
Cool, so we actually see this print out here, car four and car two are the same.
And that's because like if you look at the we can actually show the upgrades, we know that they're
going to be a.
These values will be the same because we make her to right here.
It's a white Ford Fiesta.
And then right here we say car four equals car two, so they're totally the same there.
Then what we do is we add upgrades to car two, but not to car for.
But then this right here gives us back the same, OK, so we show car for upgrades below, right?
It says one to and four, which is what Car two would have had, because in our constructor up here,
we know that we give everything of code one right off the bat, right?
We have this upgrade coast aero pushback.
One in our constructors there signed multiple constructors.
So we know that Car two, after adding to influence of the upgrades, is going to have one two in four.
And then right here on Line 17, we show what Car four has after this line with the plus operator overloaded.
And so what you can see here.
Is that at that point in time here, it's this one right here, the showing car for upgrades below,
it's the first time that we print this out and it says one two four.
So that's why we see this one two four here.
And we print out, let's see.
So show upgrades for car for.
See, and it'll have the same there, I think, actually so OK, here it no, this is a cartoon show
upgrades.
So I can go ahead and.
Well, here we can just kind of track it.
So car for show of grades, prints, the one.
Then we add two and four to car two and we say car to show a grade so that prints one, two four.
And then we have the.
Car four down here showing the upgrades, and so everything is pretty much set up.
So we probably should also show when it's not true.
Right, so we can count, we saw when it's true, they have the matching, all the matching data members
and the same upgrade codes as well.
So let's go ahead and like if we put car five.
Equals car to then these should not match so right, because Car five has added upgrade five, so that
shouldn't match there.
So I'll go ahead and save this and then I'll go and run this after compiling it, of course.
So we do this and let's scroll up a little bit.
So we noticed that this is showing car for upgrades below.
This is the first it shows twice, right?
Here's the first one, and then we don't see this printout.
That's because it's not true.
You only could say some kind of lights like change this to not equal to you.
So we could say, Well, we can't actually do that because we don't have the equal operator.
So we'll actually say else as to see out.
The cars are not the same.
And I'll actually just change this to you.
The cars are the same.
Cool.
So let me go ahead and save this and compile and run again.
OK, so we scroll up a little bit and we see the cars are not the same prints out.
And that's because Car five, even though, well, actually, no, it doesn't even have the complete
matching outfits, so it doesn't even have the same painkiller.
So it's for sure not going to match.
But even if we were to put white here well for a white Ford Fiesta, it still wouldn't work because
they don't have matching attributes.
So if I put white here and I go ahead and save this and then I compile it and run it, if we choose
up here, we noticed it still says the cars are not the same if I was to not add.
This here, like if CAR two had let's just say I comment this out, right?
And we're actually I have this one to upgrade, right, and I just add five.
So a car five is going to be pretty much the same.
And then I add five to two and I add five to car five.
Then they should be the same, right?
So let's go ahead and save this.
Let's compile it.
Let's run it.
Let's cruise up here and it says the cars are the same now.
OK, so you can actually do similar things with the other operators, so you notice there's like not
equal as well, but we can have like less than or greater than or greater than or equal to or less than
or equal to.
And you're going to end up implementing them in a very similar way.
So.
We scroll down here to the operator, the quality operator we made.
Then you could basically choose your own logic for comparing like a car, whether it was greater than
or less than and probably be very similar to our class, right, because we tried to find out what the
better car was.
But rather than returning a new car object here, like we wanted to just give the better car back,
you can actually just return of Boolean right?
Because less than and greater, then that's going to be a Boolean.
So, you know, is it less than true or false, right?
Is it greater then?
That's true or false.
So you would just return Boolean once you find out whether one is greater than the other.
You can just use something similar to this and then you can return true or false instead of the car
object so that that would kind of be like what those less than a great advance would be.
And I challenge you to definitely try to implement those yourself.
And because, you know, all these operators are very important and they come in handy a lot and they
make kind of your code base really nice because the user can go ahead and just use these basic operators
to perform operations on your abstract data types.
So with that, I will see you in the next lecture where we're going to go over a last type of overloaded
operators.
And then after that, we will kind of move on to some other object oriented programming concepts.
All right.
So with that, I'll see you in the next lecture.