The friend function is a function that is not a member function of the class but it can access the private and protected members of the class.
The friend function is given by a keyword friend.
These are special functions which are declared anywhere in the class but have given special permission to access the private members of the class.
Following program demonstrates the C++ program to find the sum of two numbers using bridge function add().
#include
using namespace std;
class addtwonumbers
{
int x, y;
public:
addtwonumbers()
{
x = 0;
y = 0;
}
addtwonumbers(int a, int b)
{
x = a;
y = b;
}
friend int add(addtwonumbers &obj);
};
int add(addtwonumbers &obj)
{
return (obj.x+ obj.y);
}
int main()
{
addtwonumbers a1;
cout<<"Sum is: "<
OUTPUT:
Sum is: 0
Sum is: 30