CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C++ DETAILS
Question :
What is a friend function & its advantage?

Posted by: GladysJeba on 10/12/2007

Contact GladysJeba  Contact GladysJeba
Category C++ Interview Questions
Rating (4.7) By 26 users
Added on 10/12/2007
Views 5091
Rate it!
Answers:

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.



 Posted by: sandeep anand    

Contact sandeep anand  Contact sandeep anand

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.



 Posted by: vignesh    

Contact vignesh  Contact vignesh

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.



 Posted by: RAGE    

Contact RAGE  Contact RAGE

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.



 Posted by: gunjan    

Contact gunjan  Contact gunjan

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 ".



 Posted by: darshanjeet    

Contact darshanjeet  Contact darshanjeet

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.



 Posted by: srilatha    

Contact srilatha  Contact srilatha

#include "iostream.h"
using namespace std;

class base
{
base() { int x =5; }
int x;
friend int function_base(int);
}

int function_base(int y)
{
base a;
return a.x+y;
}
int main()
{
cout << "value returned by function_base is
" << function_base(10);
return 0;
}

Answer: 15



 Posted by: Ashish Omar    

Contact Ashish Omar  Contact Ashish Omar

What is a Friend 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.




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.



 Posted by: THE DJ AK    

Contact THE DJ AK  Contact THE DJ AK

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 ".

* The keyword friend is placed only in the function declaration of the friend
function and not in the function definition.


* 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.





 Posted by: Satish Aware    

Contact Satish Aware  Contact Satish Aware


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
Where we use reference and where we use pointers ?Write their differences and advantages and disadvantages .
View Answer
what are the disadvantages of copy constructor??
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C++ Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12289/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

0.77