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

แชร์
ฝัง
  • เผยแพร่เมื่อ 4 ก.พ. 2025

ความคิดเห็น • 152

  • @hansandreas4743
    @hansandreas4743 9 ปีที่แล้ว +79

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

    • @LearningLad
      @LearningLad  9 ปีที่แล้ว +4

      +Hans Andreas Hestad
      thank you :)

    • @b3a7brewmp
      @b3a7brewmp 8 ปีที่แล้ว +2

      heck yeah !..

    • @zahrazaidi4095
      @zahrazaidi4095 7 ปีที่แล้ว

      sir what is the advantage of using stream insertion and extraction??

    • @kirandhamane6442
      @kirandhamane6442 7 ปีที่แล้ว +1

      +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.

    • @zahrazaidi4095
      @zahrazaidi4095 7 ปีที่แล้ว

      kiran dhamane thanks😊

  • @hollyd420
    @hollyd420 10 ปีที่แล้ว +7

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

  • @Chrisx7200
    @Chrisx7200 7 ปีที่แล้ว +7

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

    • @bmkonlinetech
      @bmkonlinetech 9 หลายเดือนก่อน

      Good but not for slow learners

  • @melisabagcvan2928
    @melisabagcvan2928 3 ปีที่แล้ว +1

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

  • @matt-g-recovers
    @matt-g-recovers 4 ปีที่แล้ว +1

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

  • @pritibasu920
    @pritibasu920 6 ปีที่แล้ว +3

    We r very very grateful to you sir ☺

  • @diana0205xd
    @diana0205xd 7 ปีที่แล้ว +1

    I finally understood it, thanks dude!

  • @TusharSahu2813
    @TusharSahu2813 2 ปีที่แล้ว +1

    thanks for perfect explanation

  • @buzzstudy
    @buzzstudy ปีที่แล้ว +1

    god bless you brother 😊solve the all doubts

    • @LearningLad
      @LearningLad  ปีที่แล้ว

      Thanks
      I'm glad that my video helped you :)

  • @sourabroy7787
    @sourabroy7787 3 ปีที่แล้ว +1

    Thanks. Really helpful video 👍

  • @aryanmahajan7295
    @aryanmahajan7295 7 ปีที่แล้ว +15

    anil shetty surely rocks! :)

  • @mkalajo
    @mkalajo 10 ปีที่แล้ว +7

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

  • @brahmadevs
    @brahmadevs 4 ปีที่แล้ว +1

    This comment section is so wholesome i love it!!

  • @reliefpfeiler0815
    @reliefpfeiler0815 6 ปีที่แล้ว +2

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

    • @LearningLad
      @LearningLad  6 ปีที่แล้ว

      my pleasure.
      keep learning :)

    • @unbroken7663
      @unbroken7663 4 ปีที่แล้ว

      HAHAH and i have project LMAO

  • @KR3WSKATER32
    @KR3WSKATER32 9 ปีที่แล้ว +1

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

    • @LearningLad
      @LearningLad  9 ปีที่แล้ว

      Eli Hammer
      thank you very much :)

  • @JustinHerchel
    @JustinHerchel 8 ปีที่แล้ว +1

    What a wonderful explanation!
    Thanks lad.

  • @HK-no9wm
    @HK-no9wm 7 ปีที่แล้ว +1

    Wonderfully explained! Thank you sir.

    • @LearningLad
      @LearningLad  7 ปีที่แล้ว +1

      pleasure.
      Keep Learning :)

  • @stefan4207
    @stefan4207 8 ปีที่แล้ว +1

    Thank you Anil!

  • @abduldaim
    @abduldaim 4 ปีที่แล้ว +1

    Good Lecture

  • @TheKingFisher98
    @TheKingFisher98 7 ปีที่แล้ว +1

    Thanks Mate, that really helped me out!

    • @LearningLad
      @LearningLad  7 ปีที่แล้ว

      Pleasure.
      keep Learning :)

  • @heybuddy_JAI_SHREE_RAM
    @heybuddy_JAI_SHREE_RAM 2 ปีที่แล้ว +4

    still very useful after more then 8 years🙃

  • @pdxbound81
    @pdxbound81 5 ปีที่แล้ว

    Excellent video! Thank you so much!

  • @laibanadeem3242
    @laibanadeem3242 2 ปีที่แล้ว +1

    Thank you anil

  • @phuzaza_the_great7173
    @phuzaza_the_great7173 4 ปีที่แล้ว +1

    thanks a lot sir

  • @AxElKo440
    @AxElKo440 7 ปีที่แล้ว +3

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

  • @Gandhiboy
    @Gandhiboy 4 ปีที่แล้ว +1

    Thank you so much ❤️

    • @LearningLad
      @LearningLad  4 ปีที่แล้ว

      You’re welcome 😊

  • @eliy5550
    @eliy5550 3 ปีที่แล้ว +1

    THANK YOU

  • @aryan7069_
    @aryan7069_ 3 ปีที่แล้ว +1

    Good one!

  • @yeshwantdesai4299
    @yeshwantdesai4299 6 ปีที่แล้ว +1

    Thanks for the help!

    • @LearningLad
      @LearningLad  6 ปีที่แล้ว

      My Pleasure.
      keep Learning :)

  • @kumarAjitesh
    @kumarAjitesh ปีที่แล้ว

    Those who have a doubt that why didn't we make (>>,

  • @husseinharajli3810
    @husseinharajli3810 4 ปีที่แล้ว +1

    Thanks!

  • @ronakrawatofficial
    @ronakrawatofficial 4 ปีที่แล้ว +1

    Great man...😀😀

  • @amit80086
    @amit80086 4 ปีที่แล้ว +1

    Which compilor is using for c++. For program execution??

    • @LearningLad
      @LearningLad  4 ปีที่แล้ว +1

      GNU C++ compiler ( g++ )

  • @ngoctintran8583
    @ngoctintran8583 7 ปีที่แล้ว +10

    Very helpful! But why we can't write like this:
    ostream operator

  • @AlejandroRodriguez-wt2mk
    @AlejandroRodriguez-wt2mk 7 ปีที่แล้ว +1

    enlighten, Thanks man.

  • @mendselson2397
    @mendselson2397 8 ปีที่แล้ว +2

    clear and concise!

  • @darshenjason3164
    @darshenjason3164 8 ปีที่แล้ว

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

  • @ed.d8065
    @ed.d8065 6 ปีที่แล้ว +1

    Thanks a lot man

  • @darkseeven
    @darkseeven 5 ปีที่แล้ว +7

    what the hack, it was very simple explained

  • @jamesstark4136
    @jamesstark4136 4 ปีที่แล้ว

    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.

  • @feitan8651
    @feitan8651 11 หลายเดือนก่อน

    thank you!

  • @xXZorganStudioXx
    @xXZorganStudioXx 8 ปีที่แล้ว

    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.

  • @samia1268
    @samia1268 4 ปีที่แล้ว

    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);

  • @mikelai7483
    @mikelai7483 4 ปีที่แล้ว

    May I know why we need & symbol beside ostream, what I mean is “friend ostream &operator >>(“

    • @mikelai7483
      @mikelai7483 3 ปีที่แล้ว

      Anil, can you reply on this ? Thanks you

  • @programmerStyle
    @programmerStyle 3 ปีที่แล้ว

    what if we put space in between characters than cin not working whats the solution then? plz ans

  • @sarathelayadath2565
    @sarathelayadath2565 4 ปีที่แล้ว +1

    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?

    • @syedmuhammadabdullah1431
      @syedmuhammadabdullah1431 11 หลายเดือนก่อน

      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?

  • @gowthamprabhu122
    @gowthamprabhu122 7 ปีที่แล้ว +1

    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?

    • @shivakhatri3271
      @shivakhatri3271 6 ปีที่แล้ว

      bro have you got your answer ?

    • @AY-uc5om
      @AY-uc5om 6 ปีที่แล้ว

      Won't work as it is ostream and istream return type already...check out

  • @aniavetisyan1641
    @aniavetisyan1641 6 ปีที่แล้ว +1

    Cool thanks !

    • @LearningLad
      @LearningLad  6 ปีที่แล้ว

      My Pleasure.
      Keep Learning :)

  • @mikelai7207
    @mikelai7207 7 ปีที่แล้ว

    What you put & beside ostream?

  • @anjaligupta-wh7gv
    @anjaligupta-wh7gv 7 ปีที่แล้ว +8

    how we can overload stream extraction and extraction without friend functions

    • @623-x7b
      @623-x7b 4 ปีที่แล้ว +1

      You can make the variables that the friend function uses public and then the functions can be defined as a normal function.

  • @KueJiin
    @KueJiin 8 ปีที่แล้ว

    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

  • @pramodkarmca
    @pramodkarmca 6 ปีที่แล้ว +1

    Awesome ......

  • @msh1233
    @msh1233 8 ปีที่แล้ว +1

    Thanks a Billion ,,,,

  • @danlee3055
    @danlee3055 7 ปีที่แล้ว

    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?

  • @PrakashBesra
    @PrakashBesra 9 ปีที่แล้ว +1

    anil, why are you using reference before operator?...any reason?

    • @LearningLad
      @LearningLad  9 ปีที่แล้ว

      +Prakash Besra
      yes.
      the operator functions should return the respected class type so that we can use them with cin and cout.

  • @adebih8126
    @adebih8126 7 ปีที่แล้ว

    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?

  • @vyasarambarran2136
    @vyasarambarran2136 11 ปีที่แล้ว

    Excellent

  • @Impulse_Photography
    @Impulse_Photography 5 ปีที่แล้ว

    can I overload the

  • @johnc4624
    @johnc4624 3 ปีที่แล้ว

    Nice work.
    Perhaps pass a const Person for the output stream operator?

  • @lubinbshahi7638
    @lubinbshahi7638 10 ปีที่แล้ว +2

    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

    • @LearningLad
      @LearningLad  10 ปีที่แล้ว +17

      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 :)

    • @willingtushar
      @willingtushar 8 ปีที่แล้ว

      kasam se ....kya explain kiya h

  • @stevenlee1726
    @stevenlee1726 9 ปีที่แล้ว +6

    why u use reference operator before word operator? why why why?

    • @blondedadnan7637
      @blondedadnan7637 9 ปีที่แล้ว +3

      +Steven Lee To prevent a copy, and pass back the exact stream that was passed in, we must return the stream by reference: ostream&

  • @iAskShahram
    @iAskShahram 4 ปีที่แล้ว

    It did not work for me. it says
    "std::ostream& time::operator

  • @obtron
    @obtron 5 ปีที่แล้ว +1

    very cool

  • @jeffreypenner7189
    @jeffreypenner7189 7 ปีที่แล้ว

    I'm trying to use the ostream

  • @yousefayman4713
    @yousefayman4713 4 ปีที่แล้ว

    "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

  • @hassankamran3480
    @hassankamran3480 3 ปีที่แล้ว

    How are you calling private data members just by objects? Shouldn't we be using getter function instead?

    • @smoban
      @smoban 13 วันที่ผ่านมา

      3 years late, but its because of the friend keyword

  • @priyashasingha5521
    @priyashasingha5521 2 ปีที่แล้ว

    Perfect

  • @govindsankarmr2716
    @govindsankarmr2716 9 ปีที่แล้ว +2

    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

    • @aryanmahajan7295
      @aryanmahajan7295 7 ปีที่แล้ว +3

      dude , he's using a pun.

    • @RajSingh-rz7wx
      @RajSingh-rz7wx 7 ปีที่แล้ว +4

      And Govind, it's "it's" not "its", so next time you go correcting others, be correct yourself.

  • @TheGoat-hr2qf
    @TheGoat-hr2qf 7 ปีที่แล้ว

    i don't understand why should we return output or input?
    HELP!!!\

  • @gastarbieter
    @gastarbieter 10 ปีที่แล้ว +2

    why are using friend functions...rather than containing everything in your class?

    • @LearningLad
      @LearningLad  10 ปีที่แล้ว +1

      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.

  • @theonlyvhk28
    @theonlyvhk28 5 ปีที่แล้ว

    Heck not hack....xplanation lvl 100

  • @vivekmesuriya6357
    @vivekmesuriya6357 2 ปีที่แล้ว +1

    Whoes here to wach this video for hackerrank box it oprator overloading explaination

  • @جيمرمصري-ق5ق
    @جيمرمصري-ق5ق 5 ปีที่แล้ว

    why do you need constructors in first place i mean you can use the overloaded insertion and extraction tha'ts their job

  • @ashutoshasthana4494
    @ashutoshasthana4494 9 ปีที่แล้ว

    why do u use '&' in both function.....

  • @symbioticproductions2591
    @symbioticproductions2591 4 ปีที่แล้ว

    why do we use: & ?

  • @notpythonics
    @notpythonics 10 หลายเดือนก่อน

    do you all memorize or understand why are we reteurnning by reference and the other implemations

  • @pockpock6382
    @pockpock6382 3 ปีที่แล้ว

    saved my ass trying to get the output of rgb values in % dec and hex

  • @Jayveersinh_Raj
    @Jayveersinh_Raj 3 ปีที่แล้ว

    What the heck, you saved my ass!! XD

  • @saadmumtazkhan1403
    @saadmumtazkhan1403 4 ปีที่แล้ว

    why are we passing by rederence

    • @notpythonics
      @notpythonics 10 หลายเดือนก่อน

      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

  • @shahirabdullah5438
    @shahirabdullah5438 8 ปีที่แล้ว

    pls try to explain why r u doing these rather than describing what r u doing it will make the video easier

    • @mukeshpathak7302
      @mukeshpathak7302 8 ปีที่แล้ว

      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

    • @shahirabdullah5438
      @shahirabdullah5438 8 ปีที่แล้ว

      thanks that helped i lot....i figured it out,,,,i was confused about the syntaxes he was using for overloading >> and

    • @mukeshpathak7302
      @mukeshpathak7302 8 ปีที่แล้ว

      haha. yup that was confusing at first for me too.

  • @PakeezaAsghar-q3p
    @PakeezaAsghar-q3p หลายเดือนก่อน

    Is topic ki smjh ku nai a rai😢

  • @dhu1090
    @dhu1090 3 ปีที่แล้ว +1

    why friend???!

    • @LearningLad
      @LearningLad  3 ปีที่แล้ว

      i was just demonstrating different ways of overloading operators.

  • @AFTstorm
    @AFTstorm 8 ปีที่แล้ว

    i dont understand this one bit

  • @newera653
    @newera653 6 ปีที่แล้ว

    Iam not intend to watch We gonna,we gonna video,

  • @karahanozen8576
    @karahanozen8576 6 ปีที่แล้ว

    Helal len saksocu

  • @awaistanvir3606
    @awaistanvir3606 6 ปีที่แล้ว +1

    bhai plzz english ma na smjaya karo hindi ma btaya karo

    • @LearningLad
      @LearningLad  6 ปีที่แล้ว

      c++ ki video series hindi me bhi jald hi banaunga.

  • @KinzaZafar-x7s
    @KinzaZafar-x7s ปีที่แล้ว

    Bakwas +time waste

  • @KarimNasser-y6k
    @KarimNasser-y6k ปีที่แล้ว

    youa arent explaining anything

  • @agoraphobiccowboy2303
    @agoraphobiccowboy2303 4 ปีที่แล้ว

    Thanks a lot man! You've really helped with this video.