Why Do We Need FRIEND Functions In C++?

7

7 Answers

Williams Dsouza Profile
Williams Dsouza answered
Basically Friend functions can be introduced in C++ for handling some specific tasks which is related to class objects.
In C++, we know that private members cannot be accessed from the outside class.
That is a non-member function cannot have an access to the private data of a class.
However there could be a situation where we would like two classes to share a particular function.
For example consider a case where two classes, manager and scientist have been defined.
We would like to use a function income_ tax () to operate on the objects of both these classes.
In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.
To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:
Class ABC
{
……
…..
Public:
……..
……..
Friend void xyz (void); //declaration
};
This function declaration should be preceded by the keyword friend.
The function is defined elsewhere in the program like a normal c++ function.
The function definition does not use either the keyword friend or the scope operator::
The functions that are declared with the keyword friend are known as friend function.
A function can be declared as a friend in any number of classes.
A friend functions, although not a member function has full access rights to the private members of the class.
 
Anonymous Profile
Anonymous answered
->  Friend function normally used for accessing private member of the class.->  With the help of the Friend function we can access different class object.-> In operator overloading, with the help of the friend function we can use primitive data type      argument and user define argument at one function.-> It is not a member function of the class.-> When we want addition,subtraction... On different class we can use friend function easily.
Meenakshi Tayal Profile
Meenakshi Tayal answered
Friend functions are neeeded to make bridges between classes and to give permission to a normal function of accessing private data of a class.
Anonymous Profile
Anonymous answered
Jigar,
(a)Let us say, In class 'A'  you are declaring  function 'Fb' to be a friend function. Do you then need to define function 'Fb' in the same class 'A' / same file ?
(b)If the answer to (a) is yes, then how does a member function and friend functions are different? The function 'Fb' could have been declared as member function also. What difference does it make to define it as friend function vs member function?
(c)are there a disadvantages of friend functions?

Thanks,
ak
Anonymous Profile
Anonymous answered
Friend functions are non-member functions, preceded by a keyword "friend". It is used for accessing the members of a class being a non-member function.
Alongbar Daimary Profile
Friend Function is needed To access Private variables obviously.
For Private data isn't accessible by non-member functions or outside classes.

Answer Question

Anonymous