If a function is defined as a friend function in C++, then the protected and private data of a class can be accessed using the friend function.

The friend function is the creative function that breaks the data hiding in an object-oriented programming language. The data hiding prevents the access of private members externally from the class. The protected members are inaccessible from the outside and can only be accessed by the derived classes.

We can declare a friend function by using the friend keyword.

friend function in cpp

Declaration of friend function in C++ :

class class_name 
{ 
   friend data_type function_name(argument/s); // syntax of friend function. 
};

The function can be defined anywhere in the program like a normal C++ function.

The function definition does not use either the keyword friend or the scope resolution operator

Also Read This :- pointer in c++

#include<iostream>
#include<conio.h>
using namespace std;
class xyz;
class abc{
   public:
   int data;
   void setdata(){
     data = 15;
   }
   friend void add(abc ob1, xyz ob2);
};

class xyz{
  public:
   int data;
   void setdata(){
      data = 83;
   }
   friend void add(abc ob1, xyz ob2);
};

void add(abc ob1, xyz ob2)
{
   cout<<"abc A + xyz A = "<<ob1.data + ob2.data;
}

void main(){
   abc a;
   xyz x;
   a.setdata();
   x.setdata();
   add(a,x);
   return(0);
}

output

abc A + xyz A = 98