Friend Function in C++

Hi welcome I am going to discuss about friend functions in C plus plus there is a concept called friend function and friend class in the session I am going to discover what is a friend function ok first of all generally functions are used for what data acting in C plus plus generally in C plus plus there is a rule what is the troll means the class data the class Private data should be accessed only with the number functions of same class ok the private date of a class should be accessed with the member functions of same class which is called Data Hiding means outer sir not allowed but there is a situation but there is a situation to classes are there ok to classes are there and their having different members now I want to access with classes data through one Member function through one function generally C plus plus not support this concept but by using this friend function we can achieve this one ok that’s why a friend function is not a member of any class simple logic but it is able to access the private numbers of those classes where it is introduced as a friend for example I am giving small example for example where having house ok and where having a family for example I am having a mobile ok I am having a mobile now I am the owner of that mobile and directly I can use this phone ok without any permission ok because of I am the owner of this for next my family member what is my family members they can I say my mobile because of their my family means same family members but others are not able to access my mobile without any permission what is this example hear me and my family directly can access the mobile phone but how does not able to access because of my mobile is private when it is private only the authorised person can have the access outer sir not a lot but I am having one friend watch this but I am having a friend actually friend means what he is not my family my family member actually is outer but he is a friend that’s why he is able to access my mobile ok without any permission because of that friend here also same suppose there is a class ok data is not available to Outer but I want to access the class data from outside then it is possible only with friend functions that’s why I simply a friend function is a non Member function clearly a friend function is a non Member function of a class but it is able to access the private date of Class ok due to this advantage is what for example I am having three classes for example I am giving Physics class hear a data member is there on here it is the white class here we get a number is there here here see data number is now I am having three classes now my work is I want to find out which one is a big is array or B or C know the point is are Private data members C plus plus release what to access the Private data here we have to write one function and to access Private data and other functions is a c function that means how many functions required for three function next to find out the big I have to write one more function that means what follows function Mein total four functions now whenever classes are more the functions also increase in the Shell program size increased it increases the program size that’s why to avoid this problem I am going to make one function as a friend now watch this here there is no function ok here there is no function here there is no function now here only one function that is big but I am introducing big as a friend and this one also friend and this one also friend that means the biggest friend for both X Y and Z classes have any friend suppose just before I said one. When his friend he’s able to access my data and he is having another two friends that means he is able to access those numbers that also know the same thing is happen when every 30 friend to xyz classes me it is able to access X class data as well as why class data as well as Jet now how many functions are required only one function due to this or program size is reduced it increase the performance of the program but here the most important thing is C plus plus main concept is what Data Hiding means from outside the class we are not able to access the data number but it is again is true that concept ok the sofa and functions are taken in function concept because of their used to access Private data from outside function means non-member form call you up to discuss discuss only the members can access the data but using friend function and non member functions ok non members can also access the Private data that’s why they are against Data Hiding concept that I use the friend functions whenever absolutely they are required ok otherwise don’t go for friend function because of their agonist Data Hiding concept next what are the rules and regulations for friend function ok friend function name of any type generally constructed the rule is what constructor means function name should be matched with class but here there is no problem at all friend function name is what it maybe ok friend function name is what it maybe there is no relation between class Naman fund function next year in constructor we are having major role what it is this should be declared that should be declared in public section but for friend function there is no with roulette all we can declare the friend function in private or public or protected wherever wherever in class we can declare that friend function in private or public or protected by Vikas logic simple it is not a member as a friend is what it is non member means anywhere you have declared the meaning is same no ok that’s why the point is we can decrease the friend function at any place in our program without changing the meaning that’s why we can declare friend function at any place there is no problem at all the next friend function declaration hear the friend function declaration should be conducted inside the class different function declaration should be conducted inside the class with friend keyword it is another most important role ok a friend function declaration should be conducted within the class using friend keyword we know that we are having three stages in a function declaration function declaration calling definition of the declaration should be conducted within the class using friend quotes and other important role that definition should be conducted outside the class and here another important role in definition don’t use the friend keyword the definition should be conducted outside the class without using friend keyboard OK it is the another most important and next we can introduce a friend function to any number of classes ok we can introduce a friend function to any number of classes next and other important thing ok actually friend function declaration is conducted inside the class definition is conducted outside the class but CPP rule is what Private data is not available outside the class but here the definition is conducted outside then how they can access that’s why the most important role and functions obviously there having objects as arguments and functions that having objects as argument because of object means what copy class copy documents that having Same data that’s why when object is possible data members of also fasted that’s why friend functions they are having objects as argument ok next here they are not members watch it friend functions are just functions friends only they are not the member then on members that’s why he has friend functions are called like the normal function excel in C plus plus release what the Member function should be called using object name followed by data operator ok friend function actually here the Member function should be called using object name using data operator but here it is a friend function means it is not a member it is non Member function that’s why it is invoked like that it is invoked like normal function don’t use the objective followed by Dot operator next 10 functions about non members that’s why they directly not able to access the database data member should be access with member functions only but here friend function is not a member non-member using the object name dot membership inside the friend function inside your friend function to access the object data we should have to use object name followed by Dot operator OK it is it is the proof for friend function accessing that’s why it is how to define a friend function and how to access the friend functions the Surfer and functions reduce program size because of instead of declaring cell functions only one friend function is enough now I will show you how to work with a friend function of common header file extra hair I am giving first small simple example class sample here i n t a, b ok I N D exam date next it is the declaration friend function declaration every friend function declaration should be preceded with friend keyword next return type what it maybe wide suppose print and just before I said actually it is a non Member function of when non Member function is there you should have to define outside the class but outside the class data members are not available that’s why you should have to send the object as the argument that’s why sample the song now watch this example here print is the function is a function and it is not going to return anything and it is what friend to this class it is friend function declaration that whenever you are going to declare a friend function first of all you should have to use friend keyword return type function and friend function definition is outside the class now that’s why object a sample is what sample is the classname ok and actually objects are defined from class name that’s why I am going to send is a class MS are coming 20 find the function and different definitions should be the function should be outside the class that’s why he is already you have discussed in petrol only in declaration area we should have to use friend keyword but now I am going for definition definition should be outside and the school is what in defination don’t use the friend keyword the sun directly return type now. Text function name is what print print and their definitions outside the soil which how to declare the object as parameter is object type sample for example yes no yes is the what object that is the object explicit of you hear me out our first of all we have to send some light on a, b x.com test.py code 29 Vashi vanities formal Member function when it is normal Member function there is no need of yes yes only a reason why because numbers are having the ability to access the data members directly without using object name but here print is what not a member it is friend only that’s why whenever you are accessing data members true friend functions compulsory how to provide object name next out equal to S datey pendle and shout be equal to is not be no function close up function now it is the class declaration part and it is a friend function Definition now you have to invoke main function now I am going for a main function ok no problem ok thank you

Speak Your Mind

*