class class_name
{
...... .... ........
friend return_type function_name(arguments);
...... .... ........
}
#include <iostream>
class Distance
{
private:
int meter;
public:
Distance(): meter(0){ }
friend int func(Distance); //friend function
};
int func(Distance d) //function definition
{
d.meter=5; //accessing private data from non-member function
return d.meter;
}
void main()
{
Distance D;
cout<<"Distace: "<<func(D);
getch();
}
Output:
Distance: 5
func()
is declared inside Distance class. So, the private data can be accessed from this function.
Ask Question