A global function that can access all the data members of a class is called a friend function of that class . To make any function a friend of some class it must be decalred with keyword friend in the class.
A friend function acts as a bridge between two classes
A function is made friend to the following two causes
1: to access private data of a class from a non member function
2: To increase the versility of overloaded operator ;
class sample
{
private:
int a;
friend void function(sample obj);
};
friend void function(sample obj)
{
int i = obj.a;
}

0 comments:
Post a Comment