What is friend function?

Answer Posted / niranjan ambati

If you want to explicitly grant access to a function that
isn’t a member of the current class then friend functions
can be useful. It allows external functions to access the
private members of a class. You can declare a global
function as a friend, and you can also declare a member
function of another class, or even an entire class, as a
friend.
The compiler ignores the access level (public, private,
protected) where friend functions are declared. Friend
functions are inherently public.
Friend function should be used as minimum as possible, but
it can be used safely in the following way.. by calling the
same class object where friend function exists.. if you
want to interact with other class then call that class
objects in friends function which interacts the private
members of the class.
class NA
{
int i;
public:
NA(int i):i(t){}
friend void fun(NA *);
};
void fun(NA *obj)
{
cout <<obj->i<<endl;
}
void main()
{
NA obj(22);
fun(&obj);
};

Is This Answer Correct ?    5 Yes 7 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

How do you answer polymorphism?

580


What is byval and byref? What are differences between them?

1693


What is polymorphism what is it for and how is it used?

576


What are the three main types of variables?

607


What is oops and its features?

594






What is the difference between inheritance and polymorphism?

594


What is abstraction with example?

609


What do you mean by overloading?

587


What is abstraction in oops with example?

780


What are two types of polymorphism?

616


What is class and example?

571


What is multilevel inheritance explain with example?

630


Why is abstraction needed?

571


What are the types of abstraction?

562


What is overloading in oops?

601