 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you stream operator overloading using friend function in C++. Before starting, you can note my information. You can connect with me by following these details. You can download my app Techimage that is available on Google Play. This app is based on various programming languages. Now coming on to the topic. In this video, stream operator overloading using friend function is demonstrated. Name operators are these insertion and extraction operators. For demonstration, I have included a header file IO stream. Then name space STD is used. Name of class is complex. It has two attributes real and imaginary. Then in public section, you can see I have implemented two definitions which are overloading. Extraction operator and insertion operator. And after that I have implemented main function. I have replaced read and print functions by overloading extraction and insertion operators. And you can see I have written this statement in main function C in C1 and C out C1. Earlier you have used this statement like this C1 dot read and this like C1 dot print. So right now I am replacing read and print functions by overloading these operators. This is extraction operator overloading and this is insertion operator overloading. So whenever we use objects with C in and C out, we have to overload these operators and I have implemented definitions for these. So first function is overloading this extraction operator. Here operator is a keyword. Its written type is void and it is a friend function. So when we call this statement, here C in and C1 both are objects. C1 is of I stream class object and C1 is of complex class object. So all these objects will be passed explicitly. So you can see here C in reference is received inside in and C1 reference is received inside C. So in and C both are reference variables. In is representing I stream class and C is representing complex class. Then C out will display enter real value message on console. Here I am using in in place of C in because reference of C in is received by this in reference variable. So I can use in here instead of C in then C dot R and C dot I is written here. So real and imaginary values will be assigned into C object. It is also a reference variable. That's why C1 will be modified automatically. Now second function is for insertion operator. So this operator is overloaded. This operator is a keyword void is written type and this function is also a friend function. So when we call this overloaded function here C out and C1 will be passed explicitly. So C out is received inside out and C1 is received inside C out and C both are reference variables. Out is representing O stream class and C is representing complex class. Here you can see instead of C out I have used out because out is representing C out. Out is a reference variable of C out object and C dot R and C dot I is displayed here. So now I am executing this code so that you can understand whether it is working properly or not. So it is asking for real value. Then it is asking for imaginary. You can see the output real and imaginary both are printed correctly. So this way if you want to overload insertion and exception operators then it is also possible with the help of binary operator overloading using friend function. Now suppose you have another object C2 and you want to read two objects together using scene like this and you want to print two objects together using C out like this. So you need to change this return type of function here return type will be ice cream reference and it will return in. So why I have done this. First this operator will be called exception operator will be called based on scene and C1. These two arguments will be passed inside this function definition after its completion. If we use void as written type nothing will be returned. So here this remaining statement will become a function call. So you can see operator and second argument is available but first argument is not available. Now I am returning in from here. So when this function will be called seen extraction operator C1 the return statement of this function calling is in. So in will be received in place of this function call and that will call another another time extraction function based on C2. So if you want to read multiple objects using a single scene statement then you have to use this return statement inside operate overloaded operator function definition. Similarly here you have to return always stream reference and at last you have to return out as always stream reference. Now I am executing this code. You can see it is asking for real value then imaginary value again it is asking for real value and imaginary values. You can see the output two and four is for first complex number four and eight is for second complex number. So friends you can read multiple objects using scene inside main as you can display two objects using see out in main function. I have implemented this concept by overloading insertion and extraction operators through binary operator overloading using friend function. I hope you have understood this concept. You can read single object as well as you can read multiple objects. You can display single object as well as you can display multiple objects. So if you want to watch more programming related videos you can subscribe my YouTube channel Sanjay Gupta underscore tax school. Thank you for watching this video.