+Zahra Zaidi Here you can treat the objects as a variable. like if you want to set an attribute of the object you need to access the variable and then put it to cin>> but here you are putting the object in the cin. so you dont have to worry about if the variables are accessible or not. otherwise you have to implement a getter and setter method, to print or to set the attributes of the objects. although, getter and setter methods are more commonly used.
Hello, i don't know if you check comments. Ii had a question. If i have my class defined in a header file, do I make the friend definition in or out of the class A { }; Or if I need to put the definition in the .cpp file how do I do that without getting an error. Thank you for your time.
I have a doubt.. since the reference of ostream has been passed to the function and any change will be reflected back to calling function, then why would you return reference variable?
because, a stream object is generally a file or screen, so it doesn't make sense calling a screen or a file by value. also, we want change to happen in our file/screen, that's the whole point of cout or any ostream output, so that's a good thing?
In microsoft visual studio, I cannot put in two parameters (i/ostream &input, Person &b), because it gers an error saying there is too many parameters.
Do I have to define those functions as friend function and make them global? Because whenever I use multiple ostream functions I get errors because of redefinition
Question..! The overload functions work even when the 'return output' and 'return input' statements are not included in their respective function definition's. So whats the point of having them anyways?
lubin B Shahi In operator overloading, if an operator is overloaded as member, then it must be a member of the object on left side of the operator. For example, consider the statement "ob1 + ob2" (let ob1 and ob2 be objects of two different classes). To make this statement compile, we must overload ‘+’ in class of ‘ob1′ or make ‘+’ a global function. The operators '' are called like 'cout > ob1'. So if we want to make them a member method, then they must be made members of ostream and istream classes, which is not a good option most of the time. Therefore, these operators are overloaded as global functions and to access the private and protected members of the class(in our tutorial Person class) we make them friend functions. Hope this helps :)
hi gastarbieter, you can write everything in your class. Here i am just demonstrating how you can overload operators using friend functions, so that i don't have to make another specific tutorial on operator overloading with friend functions.
"binary 'operator' : 'type' does not define this operator or a conversion to a type acceptable to the predefined operator " I wrote the code and I got this error, can you help me please
Shahir ABD yiu cannot use > to take input for your objects. these insertion and extraction operators knows how to work with primitive datatypes.. like taking input of int char etc. but they don't know about user defined datatypes. i.e.. you cannot use >> or
the real question is why are we returnning by reference and is it even a thing? and the answer for your qeustion is: we are passsing by reference for preformnce
What the heck, this was very well explained! :D
+Hans Andreas Hestad
thank you :)
heck yeah !..
sir what is the advantage of using stream insertion and extraction??
+Zahra Zaidi
Here you can treat the objects as a variable.
like if you want to set an attribute of the object you need to access the variable and then put it to cin>> but here you are putting the object in the cin. so you dont have to worry about if the variables are accessible or not.
otherwise you have to implement a getter and setter method, to print or to set the attributes of the objects. although, getter and setter methods are more commonly used.
kiran dhamane thanks😊
Thank you! My professor made this much more complicated than it actually was. Good job.
Good but not for slow learners
Perfect!! Nice clean simple explanation...you rock!! Thank you!!
god bless you brother 😊solve the all doubts
Thanks
I'm glad that my video helped you :)
Thanks. Really helpful video 👍
Pleasure
We r very very grateful to you sir ☺
Those who have a doubt that why didn't we make (>>,
thanks for perfect explanation
You're welcome!
anil shetty surely rocks! :)
Thank You :)
This comment section is so wholesome i love it!!
Thank you so much.Finally someone told me properly.👍🏻🌸
Most welcome 😊
I finally understood it, thanks dude!
pleasure :)
Thank you Anil!
my pleasure :)
Great man...😀😀
Thank you :)
I am impressed and grateful for such an outstanding explanation.
Thank you.
Pleasure:)
Good one!
Thanks!
what the hack, that was a good explanation Anil :) . tnx mate!
Wonderfully explained! Thank you sir.
pleasure.
Keep Learning :)
Excellent video! Thank you so much!
Good Lecture
Thank you :)
What a wonderful explanation!
Thanks lad.
pleasure :)
Great Video! I really appreciate the tutorial. *Thumbs up*
Eli Hammer
thank you very much :)
Very helpful! But why we can't write like this:
ostream operator
clear and concise!
still very useful after more then 8 years🙃
thanks
Thank you so much ❤️
You’re welcome 😊
Thanks a lot for this. Really helped me with my assignments :)
my pleasure.
keep learning :)
HAHAH and i have project LMAO
Thanks for the help!
My Pleasure.
keep Learning :)
Thanks!
Welcome!
Thanks Mate, that really helped me out!
Pleasure.
keep Learning :)
thanks a lot sir
Most welcome
enlighten, Thanks man.
pleasure :)
Awesome explanation and interpretation!!! Thank you very much Anil. Keep rocking the programmer world!!!
Thank you :)
Thank you anil
most welcome :)
THANK YOU
Pleasure
thank you!
Thanks a lot man
my pleasure :)
Very well explained..helped alot..thanks bro! :D
what the hack, it was very simple explained
thank you :)
how we can overload stream extraction and extraction without friend functions
You can make the variables that the friend function uses public and then the functions can be defined as a normal function.
Cool thanks !
My Pleasure.
Keep Learning :)
Awesome ......
thank you :)
Excellent
Thanks a Billion ,,,,
pleasure :)
very cool
thank you :)
Whoes here to wach this video for hackerrank box it oprator overloading explaination
why u use reference operator before word operator? why why why?
+Steven Lee To prevent a copy, and pass back the exact stream that was passed in, we must return the stream by reference: ostream&
Which compilor is using for c++. For program execution??
GNU C++ compiler ( g++ )
Nice work.
Perhaps pass a const Person for the output stream operator?
Hello, i don't know if you check comments. Ii had a question. If i have my class defined in a header file, do I make the friend definition in or out of the class A
{
};
Or if I need to put the definition in the .cpp file how do I do that without getting an error.
Thank you for your time.
Perfect
thanks
I have a doubt.. since the reference of ostream has been passed to the function and any change will be reflected back to calling function, then why would you return reference variable?
because, a stream object is generally a file or screen, so it doesn't make sense calling a screen or a file by value.
also, we want change to happen in our file/screen, that's the whole point of cout or any ostream output, so that's a good thing?
In microsoft visual studio, I cannot put in two parameters (i/ostream &input, Person &b), because it gers an error saying there is too many parameters.
can I overload the
its what the heck and not what the hack. i have seen this in number of ur videos. heck, not hack. apart from that awesome video. u r really good
dude , he's using a pun.
And Govind, it's "it's" not "its", so next time you go correcting others, be correct yourself.
Do I have to define those functions as friend function and make them global? Because whenever I use multiple ostream functions I get errors because of redefinition
Question..! The overload functions work even when the 'return output' and 'return input' statements are not included in their respective function definition's. So whats the point of having them anyways?
bro have you got your answer ?
Won't work as it is ostream and istream return type already...check out
Heck not hack....xplanation lvl 100
anil, why are you using reference before operator?...any reason?
+Prakash Besra
yes.
the operator functions should return the respected class type so that we can use them with cin and cout.
What you put & beside ostream?
why you use friend function rather than member function, can we use member function rather than friend function,if yes how we can use it
lubin B Shahi
In operator overloading, if an operator is overloaded as member, then it must be a member of the object on left side of the operator.
For example, consider the statement "ob1 + ob2" (let ob1 and ob2 be objects of two different classes). To make this statement compile, we must overload ‘+’ in class of ‘ob1′ or make ‘+’ a global function.
The operators '' are called like 'cout > ob1'.
So if we want to make them a member method, then they must be made members of ostream and istream classes, which is not a good option most of the time.
Therefore, these operators are overloaded as global functions and to access the private and protected members of the class(in our tutorial Person class) we make them friend functions.
Hope this helps :)
kasam se ....kya explain kiya h
How are you calling private data members just by objects? Shouldn't we be using getter function instead?
question: so i was given a header file where I was to use a virtual function operator instead of friend function; how does this change the code?
saved my ass trying to get the output of rgb values in % dec and hex
May I know why we need & symbol beside ostream, what I mean is “friend ostream &operator >>(“
Anil, can you reply on this ? Thanks you
why are using friend functions...rather than containing everything in your class?
hi gastarbieter,
you can write everything in your class.
Here i am just demonstrating how you can overload operators using friend functions, so that i don't have to make another specific tutorial on operator overloading with friend functions.
What the heck, you saved my ass!! XD
I'm trying to use the ostream
char somechar;
i need to overload >> to be able to input cin>>a; (and then is b=1) or cin>>a>>somechar>>b;
how can i do it?
why do i need to use the function as a friend
cant i use it normally as a public function as istream& operator>>(Person& object);
what if we put space in between characters than cin not working whats the solution then? plz ans
do you all memorize or understand why are we reteurnning by reference and the other implemations
It did not work for me. it says
"std::ostream& time::operator
"binary 'operator' : 'type' does not define this operator or a conversion to a type acceptable to the predefined operator "
I wrote the code and I got this error, can you help me please
i don't understand why should we return output or input?
HELP!!!\
why do you need constructors in first place i mean you can use the overloaded insertion and extraction tha'ts their job
why do u use '&' in both function.....
pls try to explain why r u doing these rather than describing what r u doing it will make the video easier
Shahir ABD yiu cannot use > to take input for your objects. these insertion and extraction operators knows how to work with primitive datatypes.. like taking input of int char etc. but they don't know about user defined datatypes. i.e.. you cannot use >> or
thanks that helped i lot....i figured it out,,,,i was confused about the syntaxes he was using for overloading >> and
haha. yup that was confusing at first for me too.
why do we use: & ?
why are we passing by rederence
the real question is why are we returnning by reference and is it even a thing?
and the answer for your qeustion is: we are passsing by reference for preformnce
Iam not intend to watch We gonna,we gonna video,
why friend???!
i was just demonstrating different ways of overloading operators.
Helal len saksocu
i dont understand this one bit
bhai plzz english ma na smjaya karo hindi ma btaya karo
c++ ki video series hindi me bhi jald hi banaunga.
youa arent explaining anything
Bakwas +time waste
Thanks a lot man! You've really helped with this video.
Glad it helped!