Friend Function in C++ Part 2 | 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 today we are going to discuss about the friend function already in previous session I have started the friend function why we need the Fred functions and friend function is a function it is not a non-member function of a class but it is able to access the private data of any class where it is introduce it as a friend generally to access the private data of a class we should have to introduce member functions means we have to use member functions but whenever we want to access the private date of a function from outside the class we should have to declare the friend function and friend functions are often Li used in operator overloading concept okay they are majorly is ring operator overloading concept and I said I have given how to introduce a friend function to single class now I am going to introduce two classes one friend function okay I am going to use one friend function with the two classes okay for example I am going to take two different classes okay now I want to compare which one object is bigger okay means which one object data is big plus okay I need two classes here suppose Class B now otherwise simply class test one actually it is called forward declaration or class declaration next another class also their class test - okay here class is to do it is the test one here f21 data is this one int a public okay here it is the class test to do and it is a test one test one is having data number a okay next while you get a it is a number function and here I am going to input the date C out enter a value seen a now it is going to read the value for this a lecture here I'm going to use a friend from friend here void function will be because of actually here I am going to do what I am going to declare two classes on his test one test you to and Casoni is having a and for example test two is having B I want to find out which one is the biggest one this a are tested to be that's why in brackets I am going to introduce both to test one and just to do now it is the friend function declaration now your compiler understands what big is a function which kind of function friend function and it is having two class type argument okay already as federal have discussed that actually friend functions often having objects as arguments that is why here big is having two objects means here right now I have either class name but from class will have to declare the objects okay now it is a test to one class later I am going for tester to now class test two here int B is a next public area wide get to be here function name get K here get me next here I want to enter the B value enter B value C in be okay now the B value also enter later we have to find out the biggest once again or how to introduce the big function here also that is why void okay first of all it is a friend function that is why first a friend why it function name is what bigger for both tests even under justa two now the meaning is what big is friend of are both test one classes fantasy to class okay fine now friend function declaration completely now we have to define the friend function here I am going to define the function whenever the definition is they don't use the friend keyword okay that is a directly start with return data type oh I it function name is what big and here we have to send two arguments now soy plus 2 1 t 1 under test 2 2 T 2 because of two objects are required okay here our program is what finding which one is big that is why here condition is like this if T 1 dot a greater than T 2 dot B see out a is big next else if T 2 dot B greater than T 1 dot a see out B is big else both are equal now function clause it is the big function definition if a is bigger than B output is raising otherwise B is big otherwise both are equal now we have to call this number function that is why here I am going to create the main function main and how many objects are required to objects this weight s T 1 T 1 and test 2 K 2 now 2 objects of this class or credit later P 1 dot get EI it is reading the value for T 1 and T 2 dot get to be it is reading the value for B now we have to find out which one is a big actually to find out the bigger we have the function big but big is a friend function thus Y frame functions never called it to the object name directly we have to go okay here it is the calling directly big of T 1 comma T 2 because of big member function is having two arguments class type arguments means objects that's what we are sending T 1 and T 2 all the arguments fergie now what happens it is faster to this one now it is going to bring the value for example Taiwan dot get it suppose it is the T 1 object and it is the T 2 object suppose here E is having 10 and B is having 20 now what happens 10 greater than 20 condition false now it goes to L spot 20 greater than 10 yes when it is true we're going to get B is big for example I have entered both the same 20 20 now 20 greater than 20 false 20 greater than 20 false now L spot executed it is saying both are equals now get CH program close it is how to introduce one friend function to different classes okay here I have used two different classes test one test to do but I have is only one friend function to compare the two different class data that is why here the major advantage is what instead of writing several functions only one function can access all the classes data that is a program size is register performances increases it is the advantage of friend function okay [Music]
Info
Channel: Naresh i Technologies
Views: 65,665
Rating: undefined out of 5
Keywords: Friend Function in C++, Friend Function, friend function program, C++ Tutorial, C++ Training, Kishore, Naresh IT
Id: JY_tXO-wppk
Channel Id: undefined
Length: 8min 32sec (512 seconds)
Published: Fri Apr 14 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.