Overloading C++ Stream Insertion, Extraction Operators | C++ Programming Tutorial

preview_player
Показать описание
In this c++ Video tutorial, you will learn how to Overload Stream Insertion and Stream Extraction Operators.

You will learn what is the syntax to overload stream insertion and stream extraction operators, how to make them to work with a user defined custom class, how to define them as friend to a class, how to write the operator function in detail with example.

Learn Programming in HINDI at our youtube channel

Catch us on SocialMedia
Рекомендации по теме
Комментарии
Автор

Thank you! My professor made this much more complicated than it actually was. Good job.

Chrisx
Автор

What the heck, this was very well explained! :D

hansandreas
Автор

Perfect!! Nice clean simple explanation...you rock!! Thank you!!

hollyd
Автор

Thank you so much.Finally someone told me properly.👍🏻🌸

melisabagcvan
Автор

I am impressed and grateful for such an outstanding explanation.
Thank you.

matt-g-recovers
Автор

god bless you brother 😊solve the all doubts

buzzstudy
Автор

This comment section is so wholesome i love it!!

brahmadevs
Автор

I finally understood it, thanks dude!

dianaxd
Автор

what the hack, that was a good explanation Anil :) . tnx mate!

mkalajo
Автор

Thanks a lot for this. Really helped me with my assignments :)

reliefpfeiler
Автор

Great Video! I really appreciate the tutorial. *Thumbs up*

KRWSKATER
Автор

What a wonderful explanation!

Thanks lad.

JustinHerchel
Автор

still very useful after more then 8 years🙃

heybuddy_JAI_SHREE_RAM
Автор

Awesome explanation and interpretation!!! Thank you very much Anil. Keep rocking the programmer world!!!

AxElKo
Автор

Thanks Mate, that really helped me out!

TheKingFisher
Автор

Very helpful! But why we can't write like this:
ostream operator << (ostream &output, Person &p);
why we must have '&' ?? Thank you in advance and sorry for my poor English.

ngoctintran
Автор

Those who have a doubt that why didn't we make (>>, <<) operator fn as member fn, you are at the right place.
Before i answer this, let us know something.

Suppose there is a binary operator (+) which is a member function of class complex and C1 & C2 are the objects of same class i.e complex.
Then writing C1 + C2 , we would mean that:- the caller object of binary(+) operator member fn will be C1 not C2.
Or in general way, left hand operand will be the caller object if the object and binary(+)operator member fn both belong to the same class. As in the above case C1 and (+) operator fn both belong to same class complex.
Now i will answer your question. We can make (<<, >>) operator fn as member function of either ostream &istream classes or Test class. Since ostream & istream classes are already defined, we cannot code in these classes and therefore cannot make (<<, >>) fn as member fn. Now the question arise is can (<<, >>) operator fn be made as member fn of Test class or not? We will answer this also but first answer this question . What's our objective behind overloading this (<<) operator?
The answer is to print the values of object( I mean value of data members of object) using cout by writing
cout << t (where t is object of Test class) correct Okay
Now assume for a moment that we have defined this operator fn as member fn of Test class
Now if we write ( cout << t ; ) this statement will not invoke the operator(<<) member fn. Why ?
Since << is binary operator and member fn of Test class. Correct. therefore it will be called or invoked by object of Test class only. Right
Now we already know that in a binary operator left hand operand invokes or call the operator fn but here ( cout << t; ) cout is the left hand operand and cout & (<<) operator member fn both belong to different class(cout:- ostream, <<:- Test class). So it will not invoke << operator fn and therefore will not work.
Now one might think that if we write t << cout ; ( now both t & << belong to same class and << is already binary operator) will it work ?
Answer is yes .
But we are already habituated with cout << a; this style (where a is primitive data type)
And we don't want to write ( t << cout ;) this way. We are comfortable in only( cout << t;) this way. And writing ( cout << t; ) will not invoke or call (<<)operator fn and therefore value will not be printed.
So in order to avoid all these we made (<<, >>) operator fn as friend function .
Thank you for your patience.

kumarAjitesh
Автор

Very well explained..helped alot..thanks bro! :D

darshenjason
Автор

what the hack, it was very simple explained

darkseeven
Автор

how we can overload stream extraction and extraction without friend functions

anjaligupta-whgv