What is friend function?

As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.

Showing Answers 1 - 16 of 16 Answers

paulson paul chambakottukudyil

  • Apr 12th, 2006
 

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.

Param01

  • Aug 29th, 2007
 

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.

Arvind

  • Oct 12th, 2007
 


Its the main feature of a class that  private member data of a class can be accessed only by the class' member functions. But there is an exception , A class can allow non-member functions and other classes to access its own private data, by making them as friends.
class Test
{
private:
    int a;

public:
    void xyz( );
    friend void display(Test);  //Friend of the class 'Test' , display() can access the                                              //private data members of the class
}

void display(Test x)
{
cout << x.a;// Class private data can be accessed.
}

Friend function is used in OOP languages which allows a private or protected function to be accessed by a member outside a class.

Any function outside a Class can be given access to work with members inside a class by simply writing a keyword
friend before it, thus making it friend of the member functions.

  Was this answer useful?  Yes

MCBod

  • Mar 31st, 2010
 

A friend function is a special function that has been granted access to the priovate members of a specific class. The class itself is responsible for declaring the friendship. It is nto affected by inheritance and is not transferable in that:

1) A function being a friend of a class does not mean that the function is a friend of any child classes

2) Where a function is a friend of a class A which is itself a friend of another class B this does not mean the function is a friend of class B

charlie02

  • Aug 9th, 2010
 

Friend function is a non-member function, which can access the private member of a given class, only if it made as the friend of that class.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions