GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C++
Go To First  |  Previous Question  |  Next Question 
 C++  |  Question 5 of 203    Print  
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.
 


  
Total Answers and Comments: 4 Last Update: June 12, 2009   
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: Arvind
 

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



Above answer was rated as good by the following members:
geeker2008, ozkan, the_ankit1987
April 12, 2006 02:06:42   #1  
paulson paul chambakottukudyil        

RE: What is friend function? &nb...
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.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 29, 2007 01:23:05   #2  
Param01        

RE: What is friend function? &nb...
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.
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
October 12, 2007 00:51:25   #3  
Arvind        

RE: What is friend function? &nb...

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


 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 0Overall Rating: +3    
June 12, 2009 08:29:11   #4  
gunjan.chandra Member Since: April 2009   Contribution: 7    

RE: What is friend function?  
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.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape