What is the use of friend function?

Answers were Sorted based on User's Feedback



What is the use of friend function?..

Answer / vijaya

Sometimes a function is best shared among a number of
different classes. Such functions can be declared either as
member functions of one class or as global functions. In
either case they can be set to be friends of other classes,
by using a friend specifier in the class that is admitting
them. Such functions can use all attributes of the class
which names them as a friend, as if they were themselves
members of that class. A friend declaration is essentially a
prototype for a member function, but instead of requiring an
implementation with the name of that class
attached by the double colon syntax, a global function or
member function of another class provides the match.

Is This Answer Correct ?    90 Yes 15 No

What is the use of friend function?..

Answer / soniya

friend function can acces private member in class

Is This Answer Correct ?    42 Yes 16 No

What is the use of friend function?..

Answer / the dj ak

by: The Dj Ak
www.thedjak.co.nr
www.the-dj-ak.webs.com
www.thedjak.webs.com


What is a Friend Function?

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.

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.

A friend function is used for accessing the non-public
members of a class.
A class can allow non-member functions and other classes to
access its own
private data, by making them friends. Thus, a friend
function is an ordinary
function or a member of another class.





Need for Friend Function:

As discussed in the earlier sections on access specifiers,
when a data
is declared as private inside a class, then it is not
accessible from outside
the class. A function that is not a member or an external
class will not
be able to access the private data. A programmer may have a
situation where
he or she would need to access private data from non-member
functions and
external classes. For handling such cases, the concept of
Friend functions
is a useful tool.





How to define and use Friend Function in C++:

The friend function is written as any other normal function,
except
the function declaration of these functions is preceded with
the keyword
friend. The friend function must have the class to which it
is declared as
friend passed to it in argument.






Some important points to note while using friend functions
in C++:

* The keyword friend is placed only in the function
declaration of the friend
function and not in the function definition.
.
* It is possible to declare a function as friend in any
number of classes.
.
* When a class is declared as a friend, the friend class
has access to the
private data of the class that made this a friend.
.
* A friend function, even though it is not a member
function, would have the
rights to access the private members of the class.
.
* It is possible to declare the friend function as
either private or public.
.
* The function can be invoked without the use of an
object. The friend function
has its argument as objects, seen in example below.






properties of friend function:

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.







Example to understand the friend function:


#include
class exforsys
{
private:
int a,b;
public:
void test()
{
a=100;
b=200;
}
friend int compute(exforsys e1)

//Friend Function Declaration with keyword friend and
with the
object of class exforsys to which it is friend passed
to it
};

int compute(exforsys e1)
{
//Friend Function Definition which has access to private
data
return int(e1.a+e2.b)-5;
}

main()
{
exforsys e;
e.test();
cout<<"The result is:"<
//Calling of Friend Function with object as argument.
}

The output of the above program is

The result is:295

The function compute() is a non-member function of the class
exforsys. In order
to make this function have access to the
private data a and b of class exforsys , it is created as a
friend function
for the class exforsys. As a first step,
the function compute() is declared as friend in the class
exforsys as:

friend int compute (exforsys e1)







disadvantage of friend functions is that they require an
extra line
of code when you want dynamic binding. To get the effect of
a virtual friend,
the friend function should call a hidden (usually
protected:) virtual[20]
member function.






What is the use of friend function?
Answer
# 1

Sometimes a function is best shared among a number of
different classes. Such functions can be declared either as
member functions of one class or as global functions. In
either case they can be set to be friends of other classes,
by using a friend specifier in the class that is admitting
them. Such functions can use all attributes of the class
which names them as a friend, as if they were themselves
members of that class. A friend declaration is essentially a
prototype for a member function, but instead of requiring an
implementation with the name of that class
attached by the double colon syntax, a global function or
member function of another class provides the match.

Is This Answer Correct ?    22 Yes 8 No

What is the use of friend function?..

Answer / cs thakur

Sometimes a function is best shared among a number of
different classes. Such functions can be declared either as
member functions of one class or as global functions. In
either case they can be set to be friends of other classes,
by using a friend specifier in the class that is admitting
them. Such functions can use all attributes of the class
which names them as a friend, as if they were themselves
members of that class. A friend declaration is essentially a
prototype for a member function, but instead of requiring an
implementation with the name of that class
attached by the double colon syntax, a global function or
member function of another class provides the match.

Is This Answer Correct ?    9 Yes 3 No

What is the use of friend function?..

Answer / ritu shekhwat.

..friend function is a special member function of the class.
..it is precided with the friend keyword.
..it is access the private data of the class.
..some situation where the private data of the one class
can require in another class we declare a function as a
friend.

Is This Answer Correct ?    6 Yes 1 No

What is the use of friend function?..

Answer / damodar narayan

Visibility

The visibility of a property or method can be defined by prefixing the declaration with the keywords public, protected or private. Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

The policy is if you are not a member, you can’t get it. But there is a certain situation wherein you need to share your private or protected data with nonmembers. ‘Friends’ come here as a rescue.

A friend function is a non-member function that grants access to class’s private and protected members.

Is This Answer Correct ?    0 Yes 1 No

What is the use of friend function?..

Answer / lisa

9

Is This Answer Correct ?    8 Yes 23 No

Post New Answer

More PHP Interview Questions

How to remove duplicate values from a PHP Array?

0 Answers  


What is difference between require_once(), require(), include()?

0 Answers  


Can we run php in apache tomcat?

0 Answers  


How to invoke a user function?

0 Answers  


What is $_ env in php?

0 Answers  






How long will it take to learn php?

0 Answers  


Explain the three different kinds of Arrays?

0 Answers  


What are traits?

0 Answers  


Explain about error handling in php?

1 Answers   INDUS, Life Infotech, Yahoo,


What is exception handling in php?

0 Answers  


What is php explain how php works?

0 Answers  


How to use the COM components in PHP?

2 Answers   HCL,


Categories