What is friend function ?

No comments

Friend Function :-

A friend function is a function that access private data variables of another class. The measure different between an ordinary class function and a member function is that the ordinary function access the object that involves the member to be passed by reference or value.

Example :-                      class sum
                                                     {
                                                         Private:
                                                          Int a,b,c;
                                                          Public:
                                                           Void get dada ()
                                                                    {
                                                                         Cout>>a>>b;
                                                                     }
                                                              Void show data ()
                                                                     {
                                                                          Cout <<”sum”<<c
                                                                         Friend  void(sum x)
                                                                      }
                                                            Void  add (sum x)
                                                                            {
                                                                                      x.c=x.a+x.b
                                                                             }
                                                                   Void main()
                                                                        {
                                                                              Sum obj;
                                                             obj.get.data();
                                                                add(obj)
                                                            obj.showdata();
                                                                        }
                                             };

0bj.c=obj.a+obj.b     if we use it in main() then it will be wrong

No comments :

Post a Comment

Subscribe

Milan Panda
Admin
About Me | Contact
Copyright 2023-2024 © Programming1011 . 🎀 Developed and Design By- Milan Panda. Happy Holi All Of You 🎀