Friend Function:-
A friend function of a class will be the friend of that class. It will not be the member of that class but it has authority to access all the member of that class even they are private or protected. A friend function can be written in any scope of the class like private, protected and public.
A friend function declaration will be in the class preceded by friend keyword and its declaration will be outside.
A friend function is not a member function (not a part of the object) of the class that means it will not be called by the object name. It is called directly by writing the name of the function with brackets.
Syntax of friend function declaration:-
friend return-type function-name(class object);
Syntax of friend function definition:-
return-type function name(class object)
{
Statements;
}
#include <iostream>
#include<conio.h>
using namespace std;
class pramod
{
private:
int a,b;
public:
friend void add1(pramod s);
void add2()
{
cout<<"Hello"<<endl;
}
pramod()
{
a=10;
b=20;
}
};
void add1(pramod s1)
{
cout<<s1.a<<endl<<s1.b<<endl;
}
int main()
{
pramod ob1;
ob1.add2();
add1(ob1);
}