2. FRIEND FUNCTION
Friend function means to share data between two class.
SYNTEX :-
class name
{
public:
void get_data()
{
cout<<“ “;
}
friend
void function ();
}
3. A friend function does not belong to the class to which it is
declared friend.
A friend function is invoked just like an other c++ function without
using the object as it is not a part of the class.
It cannot access the data members directly function. It has to
use of the object name along with the dot operator.
It can declared private, public or protected without altering the
meaning.
usually, it has object as arguments.
CHARACTERISTIC OF
FRIEND FUNCTION
5. float mean(sample s)
{
return float(s.a + s.b)/2.0;
}
int main()
{
sample X; //object X
X.setvalue();
cout<<“Mean value =“ <<mean(X)<<“n”;
return 0;
}
6. Note:- The friend function accesses the class
variables a and b by using the dot operator and the object
passed to it. The function call mean(X) passes the object X
by value to the friend function.
• OUTPUT :-
Mean value = 32.5