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

Get 9,000 Interview Questions & Answers in an eBook.


  • 9500+ Pages
  • 9000 Question & Answers
  • All Tech. Categories
  • 14 MB Content

    Get it now !!


    Send your Resume to 6000 Companies


  • 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 (2.0) By 3 users
    Added on 10/12/2007
    Views 4863
    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


    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-2009 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 | Dedicated Servers | Joke of the Day

    0.48