All right, so once again, we'll come back to another video dealing with the overloaded operators.
So in this lecture, we're going to go over how to overload the output and input operators that you
would use, see out and see in.
So yes, it is possible to do something like this scenario where we would be able to use see out with
an object like hard to use.
So you notice right now it's giving us a little error here.
So it's just saying that no operator matches these upper ends.
Upper end types are and shows you that we're trying to output a car.
So this is possible, but we haven't implemented what we need to make this happen.
What's happening is that the output stream does not know how to output our abstract data type, just
like it didn't know how to add our abstract data types together until we told it how to write.
So we're going to have to overload this operator as well.
And so let's go ahead and talk about how to do that.
So, of course, we're going to want to put a prototype in our header file here.
But this is going to be a slightly different prototype and I'll tell you why.
So I'm about to introduce another new concept for C++.
So what we need to print out our car is most likely information about it, right?
So a lot of the data members are would contain the information.
And we wanted to put these as private data members, right?
So let's say that when we see out a car, we want it to print out it's, let's say, brand model and
paint color.
All right, let's just say that's what we want to print out when we print out of the car, we want this
information to be displayed in the console to the user.
The only problem is that these are private.
And so if we're coming from some client code.
You know, how would we how would we access these, I mean, if you're thinking, well, we never had
a problem in these other operators.
You know, we were able to do plus.
Right.
And if we go in here, for example, let's go down to the plus.
We were able to refer to our data and refine because we did the scope resolution.
The only problem is that this time we do not want our overloaded operator to have the scope resolution
because it's not going to work out.
Yet we still need we still need access to the private data members.
So the reason is is because the way that this overloaded output operator works is that.
We're going to need to pass in an output stream that's going to come from me, so if I hop back over
here to Maine, if we see out the car, it expects the output stream object to be passed over to the
function as a parameter.
So kind of, you know, like like this is so what we're going to do is we're actually going to pass
this as well, right?
This will be the object that we want to print out, which in this example that I come into that would
be car too.
But the thing that we also need is we need before this, we need some other.
Parameter and that is going to be the output stream object, and since we're passing that output stream
that way, we're not really referring to the this object because that's not really an output stream,
right?
So we have to pass that and we have to use that output stream to print out the data members.
And since we have to use that alpha stream to print out the data members.
We're going to need access to those data members, and so there's a way to do this.
And it's by using a special word called Friend.
So what is friend?
Well, in this case, what we're talking about is a friend function.
So we're going to make our function here that is an overloaded operator for our output stream.
But when you put friend in front of something, it basically just gives you access to the private data
members.
So.
You might have some functions sometimes that maybe come from another class, like, let's say there
was something in your step here.
Where you were like, you know, Driver is there was some driver class and you wanted to have some function
that just, I don't know, like connect car or something like that or you relate the driver to the car,
you're able to have some other scope resolution for some other class.
Yet you can still have access to the data members of the car class, the private data members, as long
as you declare that function a friend.
So we're going to use the French word to make our overloaded output stream operator.
And so what we are going to do is we're going to put that in.
What is going to return is it's going to return the output stream back to us and that output stream
is the one that's getting passed as a parameter.
So let's see what this looks like.
So we're going to return an output stream.
So this is post stream.
To do this, I'm going to need to add this up here.
If you don't already have this, you need to add Io stream.
So since I'm not using the standard namespace, I'm going to put STD, colon, colon.
And so it's going to return and stream by reference the ampersand because it wants to return the exact
same same old stream that we're referring to.
And what this is going to be is operator like that because that's software we want to overload.
And so this is where the old stream comes from.
So it's in here.
So I'm going to say, oh, stream by reference, and I'm just going to call this outputs outs for output
stream.
And so what's happening is that we're going to take in this output stream and we're going to refer to
data members of the past object.
So I'm going to say Konst Car and OBJ, just like we have before and the goal of what we're trying to
do is refer to the private data members of this right.
Like these things down here?
Model brand paint color of the object?
And we're trying to use the output stream that's coming from Maine, so basically it would be this output
stream for sea out would be sent over here into the function as a parameter.
And the reason that we're going to need it to be a friend is the fact that we want to have access to
these private members, even though you know we're trying to refer, we want to be able to refer to
the private members of this right.
So this object right here and use it in this output stream.
And that's the reason why we need to call it a friend so it can have access to these for this object.
So let's go ahead and look at how we would implement this, so I'm going to go over to the CP file,
so I think I'm just going to add it right here above the plus operator.
So I'm going, I don't need to put the word friend in here.
I can just put friend in the header file for a prototype.
And what it's going to do is basically in the compiler is just has this list of everything, like every
method, it's going to have access to private data members and this will kind of get added to that list
every function that has access to the private data members.
Right.
And previously, we've gotten access to those via using the scope resolution.
But now we are going to just have this friend declared.
So we don't need scoped resolution and we can just start typing that we're going to return a return
type, which is O Stream, and it's going to be by reference.
And I'm going to put operator like that.
And now we can start typing out parameters that we have the stream by reference coming, and I'm going
to call it out.
And then we also have a we can make this contact because we're not going to modify the object coming
in.
We're just going to print it out.
And so that's why we can have the car object.
So this would be Konst car and object like that.
And it can be cast because we're just going to print stuff out.
We're not going to try and modify the contents of the past object.
So what can we do right here?
Well, first, I think I'd like to print out a message when we're printing out the car object and I'm
just going to say car information, something like that.
Maybe I'll just put this all caps or something car out of, say, car info like that.
And then what we can do now is we will actually use and I'm not going to you see out.
Sorry, I totally messed that up.
What I want to do is actually use out.
So we're going to this is the output stream that's being passed and it's coming from Seattle, right?
So Seattle, this sea out right here is going to get passed in here as an argument by reference, and
we're going to use that output stream here in this file, in this class, in the implementation.
And so we're using ours and so we're outputting into standard out basically like the standard sea out.
Here, I'm just putting it in line.
But then now what I'm going to do is use Alex again to print out the actual info about the past car
object, which if you look at it here in this example, it'll be car to.
So I'm going to say object dot, and now I can reference all of these things without there being problems
because this is a friend function.
So let's say, how about the brand first?
And what I want to do is probably say, let's just say brands like that.
And then I do and in line, and then I will say, let's say model actually changes to make because I
think it makes us a better word.
We have brand is right.
Yeah, we have brands, our data member, but I'm just going to say make their OK and then we can put
an object that model and then we'll do them in line and then we can do paint color.
So I'll just say color and then we'll do object paint color.
OK, cool.
So every time we print out a car object, this is where print out.
And remember that what we're returning is an output stream, so we actually need to hand back this output
stream, right?
So we're going to print this stuff out and then that's going to get sent back to where it was called
from in Maine.
So I am going to put a return out here.
And we're going to return that by reference.
Because we hear that you see there's a reference on the return tape, so obviously taking this out,
you notice this output stream by reference the data type and we're turning this.
So this is the same thing as a return time.
All right.
So let's go ahead and see if this works.
So if everything is all good, then in Maine we uncovered this.
What should happen is that it should say like, Oh, see how interesting I'm trying to see how a car
I'm going to go in here.
And since this is already declared a friend, I'm going to go ahead and go into this function right
here.
It's defined as the overloaded operator for the output stream, and I'm just going to print out whatever's
here to the output stream that was coming from Maine.
And now we can access these private members due to the way that we set this up as a friend.
Cool.
So let's go ahead and test that out, then I'm going to go ahead and save this and I will save this
file.
And I will save this file.
I could just do save all, but I just to explicitly do that.
So go ahead and compile it.
We'll go ahead and run it, and let's see what it says.
So let me go ahead and scroll up, and there we go, look, so we printed out car two.
You know this car to here when we made it, says white Ford Fiesta, right?
And.
When we do this nine, 11, it prints it out all fancy like with this information and it says car info
make Ford Model Fiesta Color White so pretty cool right now.
You have a way to just see out your abstract data types and you, as the programmer, are the one who
can define how that gets printed out.
So when the user of your code base, your library wants to, you know, do stuff with cars, they can
print it out and it will just print out this fancy thing that you made each time they try and print
a car object out.
So pretty cool, right?
Yeah, definitely pretty cool.
So the next thing that we want to do is the input stream operator because we can do the same same exact
thing for the input.
So let's head back over to our header file.
And so we're going to make something that looks very similar, except it's just going to be slightly
different.
It's still going to be a friend because I still want access to this memory.
So we're going to set in this time.
And what we're going to do actually here is Ice Stream.
Oops, I stream my reference and you know, these are just going in the other direction.
And then we're going to get an ice stream coming in, right?
And I'm going to call this ends for input stream.
And this time we're not going to put a cast.
Oh, I spelled that wrong.
This time we're not going to put a contest object because we actually want to modify it, right?
So we don't want it to be a constant.
This one, we could have it be a constant because we were just trying to print stuff out of that.
We're trying to change its data.
This time we're going to be modifying the data.
So.
Let's go ahead and say car and OBJ.
Cool.
So now we have our input stream prototype.
Let's go over here to cart that.
And I'm going to say study.
Oops!
Ice cream, ice cream.
I scream and scream and up for a tour,
and then we have our car objects.
Cool.
So now what we're going to do is the same thing that we're actually going to set these data members.
So what I can do is just have the ends here and we'll say object dot.
Let's say we just want to change the model and paint color.
What if we just change?
What if we just change like the paint color or something, we could probably do that?
So if we want to put some?
I guess we could put all three of these.
We could just change, you know, if you want to exchange your car, let's say you just want to input
a car, you have car two and then you exchange it for another car by inputting new stuff in that object
will change.
So we could say, what if we just say model and paint color?
How about that you can exchange it for a different one.
Actually, we'll put all three.
We'll put out three.
You could put the brand.
So we'll just say input to object brand.
And then we'll just say and and then we'll say object dot model, and then we'll say and and then say,
OK, that paint color closely to all these strings that will come in and then we'll go ahead and just
return our input stream.
So let's go ahead and go back to Maine.
I'm going to go ahead and save this and let's test this out.
So we're going to have to enter stuff in our program.
So we'll go down here.
Let's just say you kind of mess with car for or against Mr. Car to get odometer car, get paint.
Cool.
So we'll go ahead and put car two here.
So if we say, uh, see in and we say.
Let's put a little smile first of the FTC out
input.
Let's just say make model color for car.
Cool.
So we'll do that.
And then we'll do a speedy scene and or scene into car two.
How about that?
So let's go ahead and try this out, so I'll go ahead and save.
And then actually, what we want to do is probably let's see our car too after that.
So I should be out and I won't just say.
Let's just say a new car to think so.
Well, something like that.
And then let's see our car, too.
And
how about that?
So let's go ahead and save this, and then we'll compile it and run it, so compile that.
Let's go ahead and run it, OK?
Input make model color for the car.
So when we print it out, if we go up here.
You can see it right here.
Car info.
Ford Fiesta Way.
So now we're being asked to change that.
So let's just say Subaru, and we'll say out back and blue.
So let's see what is said.
Cool.
So we put a Subaru Space, Outback Space Blue.
Know the strings are parsed in that way between white space.
So it read that into the make model and color.
And we said new car to info below, and it printed out that nice thing.
Right now, Car two is a Subaru Outback.
That's blue.
So pretty cool, right?
We have a way to read in and print out our objects so we can read in the data into them and print them
out in whatever way we want.
So it's pretty nice that people can just simply use this operator with it now, and you don't have to
do any extra work, you don't have to set up a special function for it.
You could just have it really accessible to the user.
All right, so with that, I might be going over a few more operators, but I might actually just have
them as some homework for you all to kind of look up some of the other ones like.
Maybe doing like multiplication operator for the mathematical ones overloaded and then maybe like some
less than and greater than ones for the logical and then, you know, I kind of showed you these ones
here for the output operator, but there are still some other operators that you can do like you could
do like the Plus Plus operator, pretty much anyone that you can think of, like minus minus plus plus.
Not equal to.
We didn't do that, but they're they're all very closely related.
I've kind of just shown you the three different types like versions or I guess categories of operators,
you could say.
And that should be enough to get you going with overloaded operators.
But I'll try and include some practice problems, I think, at some point.
So it kind of depends.
We might do more of that or maybe move on to some other topics because we still have some object oriented
stuff.
OK, so hopefully that made sense and I will see you in the next lecture.