There are two classes. the private data members of those two classes need to be accessed or modified simultaneously using a common function. then this function should be declared as a friend to both the classes. when using friend function we can bridge the two unrelated classes.
Q1.) Example of Friends as Bridge .
#include < iostream.h>
class friendcl
{
private:
int a,b;
public:
friend int sum ( friendcl x);
void set( int w, int q);
};
void friendcl:: set( int w, int q)
{
a= w;
b=q;
}
int sum (friendcl x)
{
return x.a + x.b;
}
int main()
{
friendcl r;
r.set(5,5);
cout << " sum of the values is::" << sum (r);
return 0;
}