+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.
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.
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?
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?
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
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 :)
"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
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.
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
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
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😊
Perfect!! Nice clean simple explanation...you rock!! Thank you!!
Thank you! My professor made this much more complicated than it actually was. Good job.
Good but not for slow learners
Thank you so much.Finally someone told me properly.👍🏻🌸
Most welcome 😊
I am impressed and grateful for such an outstanding explanation.
Thank you.
Pleasure:)
We r very very grateful to you sir ☺
I finally understood it, thanks dude!
pleasure :)
thanks for perfect explanation
You're welcome!
god bless you brother 😊solve the all doubts
Thanks
I'm glad that my video helped you :)
Thanks. Really helpful video 👍
Pleasure
anil shetty surely rocks! :)
Thank You :)
what the hack, that was a good explanation Anil :) . tnx mate!
This comment section is so wholesome i love it!!
Thanks a lot for this. Really helped me with my assignments :)
my pleasure.
keep learning :)
HAHAH and i have project LMAO
Great Video! I really appreciate the tutorial. *Thumbs up*
Eli Hammer
thank you very much :)
What a wonderful explanation!
Thanks lad.
pleasure :)
Wonderfully explained! Thank you sir.
pleasure.
Keep Learning :)
Thank you Anil!
my pleasure :)
Good Lecture
Thank you :)
Thanks Mate, that really helped me out!
Pleasure.
keep Learning :)
still very useful after more then 8 years🙃
thanks
Excellent video! Thank you so much!
Thank you anil
most welcome :)
thanks a lot sir
Most welcome
Awesome explanation and interpretation!!! Thank you very much Anil. Keep rocking the programmer world!!!
Thank you :)
Thank you so much ❤️
You’re welcome 😊
THANK YOU
Pleasure
Good one!
Thanks!
Thanks for the help!
My Pleasure.
keep Learning :)
Those who have a doubt that why didn't we make (>>,
Thanks!
Welcome!
Great man...😀😀
Thank you :)
Which compilor is using for c++. For program execution??
GNU C++ compiler ( g++ )
Very helpful! But why we can't write like this:
ostream operator
enlighten, Thanks man.
pleasure :)
clear and concise!
Very well explained..helped alot..thanks bro! :D
Thanks a lot man
my pleasure :)
what the hack, it was very simple explained
thank you :)
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.
thank you!
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.
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);
May I know why we need & symbol beside ostream, what I mean is “friend ostream &operator >>(“
Anil, can you reply on this ? Thanks you
what if we put space in between characters than cin not working whats the solution then? plz ans
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?
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
Cool thanks !
My Pleasure.
Keep Learning :)
What you put & beside ostream?
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.
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
Awesome ......
thank you :)
Thanks a Billion ,,,,
pleasure :)
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?
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.
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?
Excellent
can I overload the
Nice work.
Perhaps pass a const Person for the output stream operator?
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
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&
It did not work for me. it says
"std::ostream& time::operator
very cool
thank you :)
I'm trying to use the ostream
"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
How are you calling private data members just by objects? Shouldn't we be using getter function instead?
3 years late, but its because of the friend keyword
Perfect
thanks
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.
i don't understand why should we return output or input?
HELP!!!\
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.
Heck not hack....xplanation lvl 100
Whoes here to wach this video for hackerrank box it oprator overloading explaination
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.....
why do we use: & ?
do you all memorize or understand why are we reteurnning by reference and the other implemations
saved my ass trying to get the output of rgb values in % dec and hex
What the heck, you saved my ass!! XD
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
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.
Is topic ki smjh ku nai a rai😢
why friend???!
i was just demonstrating different ways of overloading operators.
i dont understand this one bit
Iam not intend to watch We gonna,we gonna video,
Helal len saksocu
bhai plzz english ma na smjaya karo hindi ma btaya karo
c++ ki video series hindi me bhi jald hi banaunga.
Bakwas +time waste
youa arent explaining anything
Thanks a lot man! You've really helped with this video.
Glad it helped!