 Hello friends, I am Sanjay Gupta. In this video, I am going to demonstrate you how you can implement the concept of friend function in C++. Before starting, if you want to watch programming related videos, you can connect with me by following these details. You can download my app Takamis that is available on Google Play. You can search my channel on YouTube by typing my name Sanjay Gupta. My channel will be available there. You can open this channel and you can subscribe my channel if you want to watch various programming related videos and playlists. Now coming on to the topic. In this video, I am going to implement the concept of friend function in C++. And in this program, I will be adding values of two different classes using friend function. So first I am including header file, IO stream. Then I am using namespace std. Now I am implementing class 1. Inside this class, I am declaring a variable int a. Then in public section, I am defining read function, which will display our message, enter a number on console. Then endl is for new line. It will read the entered number inside a using scene. After reading this number, I am implementing declaration of friend function that is friend int sum 1 comma 2. Here 1 and 2 both are classes. So I will be passing two arguments of 1 and 2 classes into some function. It's return type int and it is declared using friend keyword. So this statement is friend function declaration. Now I have not defined two or class two right now. So before using this, I have to write this statement. This is advance declaration of class two. So if I declared this statement then only I am able to use this to here. Otherwise compiler will generate an error. Now I am terminating class one and I am implementing definition of class two. Inside this, I am declaring a variable int b. Then in public section, read function. Inside read, I am implementing c out and it will display a message enter a number then endl. Now to read this number, I am using scene b and similar to class one in class two. I am also declaring sum as friend function. So now you can see class one and class two both are implemented. Both are having friend function declaration. Name of function is sum and it is having two arguments one for class one and one for class two. Now after implementation of these classes, I am defining some function outside the class. You can notice that I don't have to use scope resolution operator while defining friend function outside the class because when scope resolution function sorry scope resolution operator is used. It means the function is belonging to a particular class. But in this case, sum belongs to more than one classes. That's why scope resolution operator is not required. Now I am declaring a temporary variable t of integer type then t equals to x dot a plus y dot b and then return t. So this is the definition of sum function. You can see I have x is a and b which are private members of class one and class two in sum function. So this is the benefit of friend keyword. If you declare a friend function as a member of more than one classes, then the friend function can access all the private properties of these classes. Now after this, I am going to implement main function. Inside main function, I have to declare an object of one class and another object of two class. Then I am calling a one dot read and b one dot read. After these reading function, I am defining c out result equals to and I am calling some function here and I am passing a one and b one as argument. So you can see the result of sum will be displayed here automatically in c out statement. Then I am using return zero and you can notice that no implicit argument is required for calling this sum function because it is a friend function. So this is complete definition or implementation of this code. Now I am going to execute this. It is asking first number. I am entering 10. It is asking second number. I am entering 20. You can see the result is equals to 30. So friends with the help of this friend function, I have added element of class one and class two together in a single definition. This is the benefit of friend function. I hope you have understood the concept of friend function in C++. If you want to watch more programming related videos, you can search my channel on YouTube. Here various programming languages related videos and playlists are available. Don't forget to subscribe my channel. Thank you for watching this video.