Hello and welcome to the 59 tutorial in the c++ programming series, and in this part we'll be looking at the friend function. A friend function does not actually belong to a class but can access its private and protected variable. The variable methods is that it is declared in a class declaration, but it looks like he belonged with us to show you what we mean by that and how to implement a friend function. So what we're going to do is create a simple class, call it friend function class has been called a friend function. And we're just going to create a overload the constructor is just takes in a variable which is an integer called 10. And to create a fun function, we use a keyword friend Then well we'll just put our function afterwards because our friend, right, so you do read or whatever you want it to be.
For this instance I'm going to be void. And in here, we're going to take in and our friend function object and we're going to do friend function, I'm gonna call it find fun. And then when the present private variables, actually one second, make it private, so and i void deploy. And now what we're going to do is need to call brackets that is going to do friend function colon colon friend function. Just simply implement In our constructor we do or equals temp. Now what we're going to do is implement our friend function one thing to know is you don't actually put the class name and colon content or you just implement it as if we want a function on its own so we can do void automatically the input down here so we'll do void point number and here is going to take friend function friend what this is going to do is STD C. Friend funk.io STD and long last STD right and then what we've done is two friends dot private method is going to call that as well as within the void friend function private method under STD C and Troy as the end line.
And now what we can do is just created friend function objects a friend function, call it fun passing the value of four. So or even initialized four. I'm going to do print number, I think see we access it by just putting the function name we don't do phone dot print number, because you can't actually can't actually access it that way at all because even though it's declared in here, cuz it's a friend function, it's implemented outside So the bounds of the class but you can still access the class internal methods private methods, I should say the dusky this private method and variable dates when you're trying to access those when you print number and then he'll pass in funk. Now if we run this value for and private so what we did was we was able to access funk because if we do STD C and we try and access or print out a default.or crash because it's saying either private member so even though it's a private member, this friend function was able to access it to access both the private members the variable and the method itself.
So from running again, we get four printed out I which is private, and also the method which is all Probably in this methods race impulse to the CEO. That's that's the purpose of this tutorial to show you what it is. And if we were to just get rid of friend a second. And once we got those out, they said it the private member a friend function problem method, the private member or friend function. As you can see, if we get rid of the friend, keyword here, it can't access AI and private method anymore because it's not a friend of it. So you can think of it as sort of like real life.
If you have friends in real life. You they'll probably share secrets with you that they will then share with other people. So it's sort of similar to that. That's it for this part of the series. In the next tutorial, we'll be looking at the friend class so it's an extension of the general key word friend. If you have any questions, feel free to message us or support us on our systems or co. uk email will be in the description.
You can comment on this video or historically messages via YouTube or the recording source code will also be in the description. And as usual, thanks for watching and I hope you have a one day