If you add friend before creating the funciton, then you have a friend function.
When you create a friend function,you should put it in a class, so that the system know this function is a friend of the class.
friend void SamFriend(Sam &sfo);
A friend function can basicallly use data(like object or…) other functions in this class, both from public sector and private sector.And when you write
the function outside the class, you don’t have to write “Class::yourfunction()”,because this function doesn’t belong to the class, and it’s just a friend of the function.
There is an example that I write while learning the ‘friend’ concept.
#include <iostream>
using namespace std;
class Sam{
public:
Sam(){SamVar = 0;}
private:
int SamVar;
friend void SamFriend(Sam &sfo);
};
void SamFriend(Sam &sfo){
sfo.SamVar = 99;
cout << sfo.SamVar << endl;
}
int main(){
Sam bob;
SamFriend(bob);
}