More on C++ Friend Functions (Examples & Explanation) | C++ Tutorials for Beginners #28

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

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

  • @siddheshpanajkar660
    @siddheshpanajkar660 2 ปีที่แล้ว +54

    6:40 -> It will show an error because "Y" should be forward declared. It should be declared before defining the class "X".
    15:17 -> It will not show any error.
    16:55 -> Values will not swap if "&" is removed.

  • @TheISP
    @TheISP 4 ปีที่แล้ว +60

    7:18 oh yes i forgot that the compiler doesnt know Y yet. now i will remember. Thanks Harry bhai!

    • @Mumbai_indians-11
      @Mumbai_indians-11 3 ปีที่แล้ว

      Bhai tera name Ishan pandita hai wo football player

    • @nilusingh75
      @nilusingh75 2 หลายเดือนก่อน

      @@Mumbai_indians-11 bhai tujhe kaise pata iska naam

  • @adityamaurya1737
    @adityamaurya1737 2 ปีที่แล้ว +26

    One thing I would like to suggest as your student,
    Before explaining any topic using an example, try to explain the example question first so that we could also get some idea what the use of this topic and get more interest in the topic...😁
    Otherwise, all ur content is just awesome❤❤

  • @gauravchaudhari9401
    @gauravchaudhari9401 3 ปีที่แล้ว +2

    pehle 2 vedios me friend function bohot hard lgg raha tha lekin jbb is vedio ko dhekha to Friends function 💯% msj gaya...
    Harry bhai aapne iss vedio me jese examples liye he smjane ko ... vese hi Simple examples liya karo pahle...
    agar aapne ye wale examples pahle 2 vedios me samjaya hota to iss vedio ki koi jarurat nahi thi ....
    By the way Thank you Harry Bhai...
    This tutorias playlistl help me a lot of information in C++;

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

    8:01 Thanks for giving simplest example of friend function ❤❤

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

    Hey harry, I have written the code wherein I took input from the user in two different classes, then added the numbers and printed their result using a common friend function as you instructed in the video. positive and constructive feedback is always welcomed.
    code:
    #include
    using namespace std;
    // forward declaration of the classes to avoid any unecesaary error.
    class input1;
    class input2;
    // input1 : first test class in place of X.
    class input1 {
    int x;
    public:
    void setdata(int a);
    friend void add(input1 a, input2 b);
    };
    // setdata() of input1: to set the value of x to be equal to a
    void input1 :: setdata(int a){
    x = a;
    }
    // input2: second test class in place of Y.
    class input2 {
    int y;
    public:
    void setdata(int b);
    friend void add(input1 a, input2 b);
    };
    // setdata() of input2: to set the value of x to be equal to a
    void input2 :: setdata(int b){
    y = b;
    }
    //common friend function to both the above classes
    void add(input1 a, input2 b){
    cout

  • @salilchandwadkar789
    @salilchandwadkar789 4 ปีที่แล้ว +12

    16:54
    No values will not be swap because it's Call by value. In call by value function won't return the value thta's why values won't be swap.

  • @puneetsingh9678
    @puneetsingh9678 4 ปีที่แล้ว +14

    Harry bhai you are a very hard working guy, respect!

  • @TheISP
    @TheISP 4 ปีที่แล้ว +5

    16:49 No, values will not be swapped. Then only pass by value will occur and change in values of formal parameters will not be reflected in actual parameters

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

    Thanks very much
    I am in 7th grade and i have learned many languages from you
    You are the best

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

    challenge aceepted [16:49]----> the value will not be swapped becoz the value given by you will go to first in c1 and c2 class as parameter there the friend function exchange would be there which exchange the value but that would not be shown in main function

  • @rnikhilkumar9371
    @rnikhilkumar9371 3 ปีที่แล้ว +39

    For swapping, Call by reference or call by pointer can also be used there but in case of call by pointer we need to use arrow operator instead of dot operator to access members of objects and dot operator could also be used but for that we need to use parenthesis like👉🏻(*ptr).member_name to access members of the objects.

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

      if we do the same in java or python then how

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

      @@DSMAHarshSingh Java and python don't support pointers

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

      I just learned from Jenny Ma'am ❣️

  • @ahnaf_cp
    @ahnaf_cp 22 วันที่ผ่านมา

    // Always forward declaration
    class Y;
    class X
    {
    public:
    int data;
    void setValue(int value)
    {
    data = value;
    }
    friend void add(X, Y);
    };
    class Y
    {
    public:
    int num;
    void setValue(int value)
    {
    num = value;
    }
    friend void add(X, Y);
    }; --> 6:42

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

    17:18 > Value won't be swap as because function always passes a copy in general , but referencing will swap it as because they got their address

  • @SweetyTripathi-s5t
    @SweetyTripathi-s5t 2 หลายเดือนก่อน +2

    You're such a great teacher sir..❤

  • @drajkishorepatro2864
    @drajkishorepatro2864 4 ปีที่แล้ว +80

    6:35 there will be error because class Y should be forward decleared.
    15:14 no error
    16:56 vlaues swap nai hogaa

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

      poor harry wanted 3 separate comments :(

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

    17:16 Values will not swap :- u will have to use call by reference either pointer or reference variables

  • @rajeshwarshah_indian
    @rajeshwarshah_indian 5 หลายเดือนก่อน

    Thank you sir for this great explanation ❤ everything is easy because of you ❤️ 19:14

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

    14:28 becoz we've not written friend function in both the class and also after writing the friend function we have to give forward declaration of class c2

  • @legend_1024
    @legend_1024 7 หลายเดือนก่อน

    6:46 yes it will throw error as when compiling the line no. 19 the compiler will say what is y so we have to forward declare y before x

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

    6:30 yes it will give error as class Y is declared afterwards and compiler won't be able to access it while it is still in the class X .
    17:00 it will not swap

    • @Aditya-mw2ru
      @Aditya-mw2ru 3 ปีที่แล้ว

      In which video he taught about string or reversing of number ?

  • @AayushBirla-y4n
    @AayushBirla-y4n 26 วันที่ผ่านมา

    This is one of the easiest tutorials in this video playlist

  • @NishilPatel-d2m
    @NishilPatel-d2m ปีที่แล้ว

    17:10 -> the values will not get exchanged as the removal of '&' operator makes the function call by value. Where the copies of object parameters are made

  • @Puneet-bc6df
    @Puneet-bc6df 4 ปีที่แล้ว

    6:28 yes, error throw krega . Kyuki uper friend function declare krte time usme Y bhi h to pahle Y class ko define krna padega

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

    17:09 Values wont be swapped because the function cannot change the actual value of the driver function , Instead it just makes a copy of the value.

  • @AhmedRaza-ty7zq
    @AhmedRaza-ty7zq ปีที่แล้ว +1

    6:36 This program will throw an error because of not declaring it as a forward declaration.
    15:00 This will not throw any error.

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

    06:31 yes it will as we havent declared y and are using it in function so it will

  • @Newone-io6og
    @Newone-io6og 5 หลายเดือนก่อน

    Harry bhai, mai bhi appke jaise hardworking hu, mai bhi sab bahot jaldi seekh jata hu. Thanks for reading this

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

    17:10 values will not swap because copy of actual parametrs has passed there will be no change in actual parameters...

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

    Bro ur examples are god damn funny but for easy to understand.Bcz of i can easily learn C++.
    Huge respect to u.......

  • @Khush-f6s
    @Khush-f6s 5 หลายเดือนก่อน +1

    Thankyou Harry for this video the concept of friend function and friend class was really clear

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

    bhai main kabhi kabhi itna aalsi ho jata hun ki aapna pur asource code hi copy kar leta hun . Love your teaching style harry bhai

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

    17:08 the values here won't swap because you explained in the reference variables video that it will just copy the value. That's why the values won't swap.

  • @rajeshwarshah_indian
    @rajeshwarshah_indian 5 หลายเดือนก่อน

    6:45 yes it will throw the error because the compiler doesn't know the class Y

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

    [17:12] The values will not be swapped as only an instance of an object will be passed and not the object

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

    can you create live sessions for this ?
    you are the teacher who understand what student thinking about doubt virtually.
    A lot of students watch your videos to clear their doubt.
    there is 0.000000000001% possibility those students who are in computer engineering field don't know you.
    Keep going

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

    6:40 cause we have to declare class Y before X to tell compiler that when u execute class Y will come somewhere in the program

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

    6:28 @CodeWithHarry Yes! this program will throw error because, while going through the code `friend int add(X o1, Y o2);` in class X, the compiler will ask ---- "what the heck is this class Y? never heard of it!" --- In short, we need a forward declaration of class Y before defining class X to remove that error.
    15:17 @CodeWithHarry Nahi ji koi error nahi aaega!
    16:49 @CodeWithHarry Ji nahi values swap nahi hongi.
    Thank you for your lectures and sincere effort to help people like us😊

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

    16:52 No, as those variables will be of same name but different address, so It wont affect the arguments

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

    This is the best video I have ever seen Harry Bhai... Thank you very very much... The example of permission taken by android app is the best example.. I think anyone who are watching this video carefully will never forget about friend functions.

  • @I_coder
    @I_coder 9 หลายเดือนก่อน +2

    yes because friend void add(x o1,y o2) yah par compiler ko y class ke baare me pata nahi hai; first we declare class y;
    and use it

    • @afafshahzadafafshahzad7755
      @afafshahzadafafshahzad7755 8 หลายเดือนก่อน

      But kia Hum class y k Liye forward declaration ni krein ge? Q k use Bhi X ka ni pata...to hum kia class X ni likhein ge??class y me forward to k Liye

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

    17:20 nhi hongi swap because call by value hota hai basically it copies the value first and then swaps it

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

    16:57
    The value will not swap because we Removed the '&' and it means that we are taking the parameters in call by value and in call by value we cannot change the actual value .

  • @DaniyalSaqib-my1us
    @DaniyalSaqib-my1us ปีที่แล้ว +2

    The swapping method name is BUBBLE SORT ALGORITHM, LEARNED THIS IN MY A LEVELS

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

    6:46-> Ye error show krega kyuki Y ko phle declare krna chahiye tha
    15:12 : No it will not show any error
    17:02 : Nahi values swap nhi hongi agar & hata diya toh

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

    challenge accepted :time[14:03]--> "Error is comming becoz we cant access private data from outside function which is exchange.....(Solution) --> so here we would use Friend function.

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

    In my opinion, the values of val1 and val2 will not swapped if we do not used '&' since the reference to the memory location of the variables val1 and val2 will still be the same. Thus, we should not be having any swapped values.

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

    1st qstn answer..forward declaration of Y...
    2nd answer....no error will come as you have made a friend function
    3rd answer....no swap as we are swapping by call by value so whatever changes we made will not be reflected into actual args...

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

    6:40 yes y ka forward declaration Karna padega
    17:00 values exchange nhi hogi because value ki copy pass hogi address/reference nhi!

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

    16:46
    no the values will not swap because we want to give reference
    without reference the value will not swap

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

    6:41 yes it will throw error as there should be a forward declaration of class Y before class X.

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

    6:18 we have to declare class Y; before class X because we are using object Y in the function(where we declared add as friend ) inside X
    values swap nahi hogi
    kyonki function end hota hai to all argument will get destroyed

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

    17:08 --> it won't work since the arguments are just a copy of the actual values, so the numbers won't swap

  • @_vaibhavkadav_
    @_vaibhavkadav_ 7 หลายเดือนก่อน

    17:06 no swapping of values happens because the actual parameters were not exchanged

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

    Your teaching skills are fabulous

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

    If we remove ambpercent values will swap Inside fxn but the changes didn't reflect back in main fxn.#call by value.😁 I think so if I am correct then Harry bhai give heart else comnt me and correct my mistake. Pls

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

    best ever videos on OOP ...

  • @आचार्य_चाणक्य
    @आचार्य_चाणक्य 3 ปีที่แล้ว

    18:56 Thanks bro!!!!

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

    Now after this 3rd video I am 100% clear on Friend function, Thankss a Lott!!!

  • @ArunKumar-jg8gx
    @ArunKumar-jg8gx 3 ปีที่แล้ว

    Jab Harry bhaiya padhaye to complex example bhi simple lagta hai😍🤗

  • @AhmedRaza-ty7zq
    @AhmedRaza-ty7zq ปีที่แล้ว +1

    17:07 Without '&' operator the values will not swap.

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

    16:57 / 19:29
    The value will not swap.

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

    16:56 Values will Not get swapped. Since we have passed Copies of values inside the variable; and not variable(or address of var using pointer) itself.

  • @Souravkumar-iw7gy
    @Souravkumar-iw7gy 4 ปีที่แล้ว +56

    class Y ko forward declaration krna padega

    • @shankarshankar5298
      @shankarshankar5298 3 ปีที่แล้ว +11

      To krna

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

      @@shankarshankar5298 Tara do hat Kiya Kam pa laga ha tuuuu kor naa

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

    Best bhai
    I Have watched all your videos 2 - 3 times

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

    6:43 Compiler does not know the existence of Y in class X when declaring friend function, hence we need to forward declare class Y; before class X

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

    Sir ap c language and c++ ko aur deep me bhi sikhaoo naa please because ur teaching style is excellent. And every word is understandable.

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

    Very helpful video sir 🙏🙇

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

    14:01 The error is coming because the display function has cout statement in its defination. So no need of cout again for printing the exchange value

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

    06:30 class Y ka prototype declaration karna hoga(forward declaration)

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

    Thank bhai yeh video ne mera friend ka concept clear kr diya ,thanks harry bhai

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

    this is better example than last two videos example

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

    16:50 actual values swap nahi honge bas exchange function me formal value copy hogi aur voh change hogi

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

    6:32 it would throw an error because Y class is not defined above

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

    Thank you very much aise hi aur best videos banate rahiye hum students ke liye

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

    16:15 swap nahi hoga, kyuki it is call by value. Aapne c lang ke vdo me bataya tha

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

    KEEP UP THE GOOD WORK BUDDY !! HOPE YOU A GET A MILLION subs. SOON.......

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

    16:54 values will not swap because call by value is used;

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

    6:28 / 19:29
    This program will throw an error.
    Thank you harry sir

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

    Error
    13:56 Friend function not called; hence exchange cannot access the private members of c1 and c2.
    15:26 No Error

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

    Thank you sir I understand perfectly.

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

    In this video I answerd all of question ( in my head) but harry vai deserved the thank you cus i learned from your videos

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

    15:28 - iska matlab friend function ko read/write dono ka access mil jayega class variables ka!

  • @coderkartik-t9x
    @coderkartik-t9x 2 หลายเดือนก่อน +3

    harry you didn't explain operator overloading in this playlist

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

    6:39
    Yes it will throw an error because there is no forward declaration of the class y which is being used in class x inside declaring friend function.

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

      Which cource did you chose

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

    Harry bahi this is very good lecture series I learn a lot from this

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

    Thank you harry bhai you are doing great work and you will reach 4 million soon Inshallah

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

    Without using reference variable(&) the value is exchanging
    #include
    using namespace std;
    class c2;
    class c1{
    int value1;
    friend void swapped(c1,c2);
    public:
    void setdata(int num1){
    value1=num1;
    }
    void display(){
    cout

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

    17:06 No the values will not swap after removing the referencing operator.

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

    Time : 17:3
    The value is not swap because vales are not exchange , address is changed

  • @Techny
    @Techny 4 ปีที่แล้ว +8

    amazingg sir!!!!

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

    harry bhaiya you are great saviour.

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

    6:27 yes error aayegaa... kyunki Y class ko declare nai kiye hai pehle... X class ke andar Y ke baare me baate ho rahi hai

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

    17:02 nhi exchange hogi value because address ( &
    )ke bina aap value change nhi kr sakte ji

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

    yes it will produce a error
    resign: when a compiler start compiling the code it will start compiling from first line of code and it firstly read class X and inside the class X there is friend function declared (friend void add(x , y)) in this case compiler alraidy knows what is means on X in declaration of friend function but it still not know what is Y.
    solution: To solve the error make a pre-declaration of class Y

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

    6:30 no errors because now all private part of X and Y can be accessed
    15:27 no errors
    16:57 no swap since there is no direct relationship they just passed the value

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

    6:35 shows an error
    15:15 no error
    16:55 not swap, because it is pass by value not by reference

  • @AshutoshKumar-fu6qe
    @AshutoshKumar-fu6qe 3 ปีที่แล้ว +1

    16:55 obviously swap nahi hoga. Aapne rohan ko apne notes ki copy jo di hai, original thode hi diya.

  • @sanabatool7021
    @sanabatool7021 4 หลายเดือนก่อน

    We love you harry bhai….❤

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

    6:32:00 yes it will throw error i think. because of inaccesibility