Friend Function in C++ Part 1 | C++ Tutorial | Mr. Kishore

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hi welcome to nourish 80 this is Kishore and now I am going to discuss about friend functions in c++ there is a concept called friend function and friend class now in this session I am going to discover what is a friend function okay first of all that generally functions are easy for what data axing in c++ generally in c++ there is a root what is the rule means the class data the class private data should be accessed only with the number functions of same class okay the private data of a class should be accessed with the member functions of same class which is called data hiding means authors are not allowed but there is a situation but there is a situation two classes are there okay two classes are there and they are having different members now I want to access both two classes data through one member function through one function okay generally see place is not suppose this concept but by using different functions we can achieve this one okay that's why a friend function is not a member of any class simple logical but it is able to access the private members of those classes where it is introduced different for example I am giving small example for example we are having a house okay and we're having a family for example I am having a mobile okay I am having a mobile now I am the owner of that mobile and directly it can use this form okay without any permission okay because of I am the owner of this form next my family members what it is my family members they can access my mobile because of their my family means same family members but authors are not able to access my mobile without any permission watch this example here me and my family directly can access the mobile phone but otters are not able to access because of my mobile is PI wait that's why when it is private only the authorized person can how the access outer circle not tell but I am having one friend watch this one but I am having a friend actually friend means what he is not my family member he's not my family member actually he is outer but he is a friend that's why he is able to access my mobile okay without any permission because of the friend okay he also say suppose there is a class okay now Otis in class data is not available to Otis but I want to access the class data from outside then it is possible only with the friend functions that is why it 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 data of class okay due to this advantage is what for example I am having three classes for example I am giving suppose it is the X class here a data member is this and here it is the white class here B data member is there here Jada class here C data number is less now I am having three classes now my work is I want to find out which one is a big either A or B or C now the point is ABC are private data members now sepia special is what to access the private data here we have to write one function and to access this private data another function next to access the see another function that means how many functions required here three functions are required next to find out the big I have to write one more function that means what one more function means total four functions now when our classes are more the functions also increase in that's why program size increases now 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 okay 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 now the big is the friend for both X Y and a jet classes when I friend suppose just before I said one point I am having a friend when his friend he is able to access my data and he is having another two friends that means he is able to access those numbers get also know now the same thing is happening and whenever it is friend to XY jet classes mates it is able to access X class data as well as Y class data as alleged now how many functions are required only one function due to this or program size ease reduce it increase the performance of the program but here the most important thing is C plus this main concept is what data hiding means from outside of the class we are not able to access the data members but it is again is to that concept okay the sufferance functions are again is to friend function concept because of de ellos to access private data from outside functions means non-member functions that is why only up to this class we have discussed only the numbers can access the data but using friend functions non non member functions okay non member functions can also access the private data that is why they are against data hiding concern yes I use the friend functions whenever absolutely they are required okay otherwise don't go for friend function because of their against data hiding concept next what are the rules and regulations for friend functions okay now for any function name of any type generally constructed the rule is what constructor name means function name should be matched with class name but here there is no problem at all friend function name is what it may be okay and function name is what it may be there is no relation between class name and fun functioning next here in constructor we are having major rule what it is they should be declared they should be declared in public section but for fun function there is no written rule at all we can declare the friend function in private or public or protector wherever variable in class we can declare the friend function in private or public or protected why because logic simple it is not a member now actually friend is what it is non-member means anywhere we have declared the meaning is same now okay that's what the point is wherever we can decrease the friend function at any place in our program without changing the meaning the size we can declare friend function at any place there is no problem at all next friend function declaration here the friend function declaration should be conducted inside the class a friend function declaration should be conducted inside the class with the friend keyword it is another most important role okay a friend function declaration should be conducted within the class using friend keyword we know that we are having three stages in a form function declaration function declaration calling the function now the declaration should be conducted within the class using friend keywords that's another important rule definition should be conducted outside the class and here another important role in definition don't use the friendly world the definition should be conducted outside the class without using friend key world okay it is another most important and next we can introduce a friend function to any number of classes okay we can introduce a friend function to any number of classes next another important thing okay actually friend function declaration is conducted inside the class definition is conducted outside of the class but CPP rule is what private data is not available also to the class now but here the definition is conducted outside of the class then how they can access that is why the most important role is friend functions obviously they are having objects as arguments friend functions they are having objects as arguments because of object means what copy class cabina copy means they are having same data now that is why when object is positive means data members are also faster that is why friend functions they are having objects as arguments okay next here they are not members watch it friend functions are just functions friends only they are not the numbers they are non-members that is why here friend functions are called like the normal functions actually in C plus this rule is what the member function should be called using object name followed by dot operator okay the friend function actually here the member function should be called using object name using dot operator but here it is a friend function means it is not remember it is non-member function that is why it is invoke like the it is invoked like the normal functions do not use the object name followed by dot operator next 10 functions are what non-members that is why they directly not able to access the data bus we know that data member should be accessed with the number functions only but here friend function is not a member on member that's how to access means using the object name dot membership operator inside the friend function inside the friend function to access the object data we should have to use object name followed by dot operator okay it is it is the tool for friend function accessing that is why it is how to define a friend function and how to access the friend functions okay thus a friend functions radius program size because of instead of declaring several functions only one friend function is and now I will show you how to work with a friend function all common hydrofoil extra Here I am giving first a small simple example class sample here int a comma B okay now I and here come up with data members next it is the declaration in function declaration every friend function declaration should be procedure with the friend key world next return type what it may be void suppose print and just before I said actually it is a non-member function of this class well 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 and the argument that's my sample so now watch this example here print is the function like when D is a function in and it is not going to return anything and it is what friend to this class it is friend function declaration beside whenever you are going to declare a friend function first of all you should have to use friend keyword can type function name under friend function definition is outside of the class now that's what object here sample is what sample is the class name okay and actually objects are defined from class name that's why I am going to send a class in as argument no I need to define the function and this one definition II should be definition should be outside of the classes that is why here already we have discussed for that rule only in declaration area we should have to use friendly keyword but now I am going for definition definition should be outside under the rule is what in definition don't use the friend keyword that's what directly written type no void okay next function name is what print suppose print and here definition is out sign that's why we have to declare the object means parameter is object type sample for example yes now it is the what object this is the object explicit object not here see out or first of all we have to send some light on a comma B that's why yes dot a equal to 10 s dot B equal to 20 now watch here when it is not normal number function when it is a normal member function there is no need of SS only a B is enough why because numbers are having the ability to access the data members directly without using object neck but here print is what not a member it is friend only that is why whenever you are accessing data members through friend functions compulsorily how to provide the object in a next C out a equal to s dot a and L and C out B equal to does not be no function close okay function plus now it is the class declaration part and it is the friend function definition now we have to invoke this simple main function now I am going for a main function sample okay suppose yes no problem here object names already s is there now and we can declare as here also next CLR is here common unlettered now it is a friend function now okay rent is the friend function that means it is not remembered when it is member we should have to invoke with the object name but it is not yet not remember means non-member that is why it is called like a normal function print okay in bracket we have to send the object name let us all now print is called in bracket what we are sending yes now it is the actual parameter it is received by this formal parameter okay that is why here what happened it is the s object okay if is having a B now this one is possible to this yes okay now s dot K becomes 10 and s dot B becomes 20 okay letter C out a and B values output is a value 20 10 and B value 20 that's it now get CH libram knishes actually in this example I have taken only one class because of it is our first example how to define a string function okay that's why I have introduced the friend function to only one class in next session I will show you how to work with the multiple classes using friend function okay thank you thank you for watching [Music]
Info
Channel: Naresh i Technologies
Views: 135,271
Rating: undefined out of 5
Keywords: Friend Function in C++, Friend Function, friend function program, C++ Tutorial, C++ Training, Kishore, Naresh IT
Id: V-uIlFcTGdg
Channel Id: undefined
Length: 17min 37sec (1057 seconds)
Published: Thu Apr 13 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.