CoolInterview.com - World's Largest Collection of Interview Questions
Home| Ask Questions| About Us| Feedback| Contact Us|
 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

  • COOLINTERVIEW.COM LANGUAGES C++ DETAILS



    Question :
    What is a friend function & its advantage?

     Posted by: GladysJeba    Date: 10/12/2007

    Contact Author  Contact Author


    Answer :
    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 Author  Contact Author
    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 Author  Contact Author
    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 Author  Contact Author
    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 Author  Contact Author
    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 Author  Contact Author
    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 Author  Contact Author
    #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 Author  Contact Author

    © CoolInterview.com
    Today's Special: Get Free Magazine from SAP now!

    A D V E R T I S E M E N T








    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

       


    Rate the above answer. Help us to know about the answer.
    Category C++ Interview Questions
    Rating (2.0) By 3 users
    Added 10/12/2007
    Views 4680
    Rate it!
    Free Offers 300 Free Magazines for you. No credit card required. No Shipping Charges. Absolutely Free for 1 Year.

    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


    Sponsored Links  

    Google Search

    Google

    CoolInterview.com is a part of Vyom Network.
    Copyright ©2003-2009 Vyom Technosoft Pvt. Ltd., All Rights Reserved. Privacy Policy
    Page URL: http://www.coolinterview.com/interview/12289/Default.asp?cachecommand=bypass


    Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial

    0.98