if we want to write same function/s with the same body in two or more than two classes. We use the keyword friend in front of the that function declaration in the class. by using friend function we can reduce the size if program. example:- there are two classes EMPLOYEE and MANAGER. and there is a function INCOMETEX. every employee and manager have to pay incometax.. in such case we use friend function infront of the INCOMTEX function.
A friend function is a non member function of a class, that is declared as a friend using the keyword "friend" inside the class. By declaring a function as a friend, all the access permissions are given to the function.
friend function can be used where same body code is required for 2 classes. it is accessed as normal function and not like other class member functions. HOWEVER U CAN ACCESS PRIVATE DATA OF CLASS USING A FRIEND. this is a serious breach to the data hiding feature of c++ as external variable are accessing CLASS MEMBERS and use of friend fuction SHOULD BE AVOIDED as much as possible.
a friend function is a special function in c++ which inspite of not being member fuction of a class has privalage to access private and protected data of a class.
properties of ff:
1. if a function to be made friend of a class than it should be declared within body of the class priciding with keyword friend.
2.freind function never breaks the security.
3.it should not be defined in name of class nor scope resolution operator is used in it's defination even the keyword freind is also not used while defining friend function.
4.when friend function is called nither name of object nor dot operator is used. however it may accept the object as argument who's value it want's to access.
5.it doen't matter in which section of the class we have declared a freind function.
A friend function is a function, which have a direct access to the class data members(either private or public) from outside the class. It is declared within the class with a prefix " friend ".
friend function is the non memberfunction of the class.By using this function we can access the private member of the class and we can perform some task.