CoolInterview.com - World's Largest Collection of Interview Questions & Answers, FAQs, queries, sample papers, exam papers, dumps, what, why, how, where, when questions
Our Services
Get 9,000 Interview Questions & Answers in an eBook.




Get it now !!
Send your Resume to 6000 Companies

Search Interview Questions

Question: What is a friend function & its advantage?

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.<br>example:-<br>there are two classes EMPLOYEE and MANAGER.<br>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.



Category Templates in C++ Interview Questions & Answers - Exam Mode / Learning Mode
Rating (0.3) By 6004 users
Added on 10/12/2012
Views 59455
Rate it!
Question: What is a friend function & its advantage?
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.<br>example:-<br>there are two classes EMPLOYEE and MANAGER.<br>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. Source: CoolInterview.com

Answered by: sandeep anand | Date: 10/15/2007 | 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. Source: CoolInterview.com

Answered by: vignesh | Date: 10/16/2007 | Contact vignesh Contact vignesh

friend function can be used where same body code is required for 2 classes.<br>it is accessed as normal function and not like other class member functions.<br>HOWEVER U CAN ACCESS PRIVATE DATA OF CLASS USING A FRIEND.<br>this is a serious breach to the data hiding feature of c++ as external variable are accessing CLASS MEMBERS<br>and use of friend fuction SHOULD BE AVOIDED as much as possible. Source: CoolInterview.com

Answered by: RAGE | Date: 11/6/2007 | Contact RAGE Contact RAGE

a friend function is a special function in c++ which inspite of not being member fuction<br>of a class has privalage to access private and protected data of a class.<br><br>properties of ff:<br><br>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. <br><br>2.freind function never breaks the security.<br><br>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.<br><br>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.<br><br>5.it doen't matter in which section of the class we have declared a freind function.<br><br> Source: CoolInterview.com

Answered by: gunjan | Date: 11/12/2007 | 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. <br>It is declared within the class with a prefix " friend ".<br> Source: CoolInterview.com

Answered by: darshanjeet | Date: 1/15/2008 | 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. Source: CoolInterview.com

Answered by: srilatha | Date: 3/2/2008 | Contact srilatha Contact srilatha

#include "iostream.h"<br>using namespace std;<br><br>class base<br>{<br> base() { int x =5; }<br> int x;<br> friend int function_base(int);<br>}<br><br>int function_base(int y)<br>{<br> base a;<br> return a.x+y;<br>}<br>int main()<br>{<br>cout << "value returned by function_base is <br>" << function_base(10);<br>return 0;<br>}<br> <br>Answer: 15 Source: CoolInterview.com

Answered by: Ashish Omar | Date: 8/29/2008 | Contact Ashish Omar Contact Ashish Omar

What is a Friend Function?<br><br>A friend function is used for accessing the non-public members of a class. <br>A class can allow non-member functions and other classes to access its own <br>private data, by making them friends. Thus, a friend function is an ordinary <br>function or a member of another class. <br><br><br><br><br><br>Need for Friend Function:<br><br>As discussed in the earlier sections on access specifiers, when a data <br>is declared as private inside a class, then it is not accessible from outside <br>the class. A function that is not a member or an external class will not <br>be able to access the private data. A programmer may have a situation where <br>he or she would need to access private data from non-member functions and <br>external classes. For handling such cases, the concept of Friend functions <br>is a useful tool. <br><br><br><br><br><br>How to define and use Friend Function in C++:<br><br>The friend function is written as any other normal function, except <br>the function declaration of these functions is preceded with the keyword <br>friend. The friend function must have the class to which it is declared as <br>friend passed to it in argument. <br><br><br><br><br><br><br>Some important points to note while using friend functions in C++:<br><br> * The keyword friend is placed only in the function declaration of the friend <br> function and not in the function definition.<br> .<br> * It is possible to declare a function as friend in any number of classes.<br> .<br> * When a class is declared as a friend, the friend class has access to the <br> private data of the class that made this a friend.<br> .<br> * A friend function, even though it is not a member function, would have the <br> rights to access the private members of the class.<br> .<br> * It is possible to declare the friend function as either private or public.<br> .<br> * The function can be invoked without the use of an object. The friend function <br> has its argument as objects, seen in example below.<br><br><br><br><br>Example to understand the friend function:<br><br><br> #include<br> class exforsys<br> {<br> private:<br> int a,b;<br> public:<br> void test()<br> {<br> a=100;<br> b=200;<br> }<br> friend int compute(exforsys e1)<br><br> //Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passed <br> to it<br> };<br><br> int compute(exforsys e1)<br> {<br> //Friend Function Definition which has access to private data<br> return int(e1.a+e2.b)-5;<br> }<br><br> main()<br> {<br> exforsys e;<br> e.test();<br> cout<<"The result is:"<<br> //Calling of Friend Function with object as argument.<br> }<br><br>The output of the above program is<br><br> The result is:295<br><br>The function compute() is a non-member function of the class exforsys. In order <br>to make this function have access to the <br>private data a and b of class exforsys , it is created as a friend function <br>for the class exforsys. As a first step, <br>the function compute() is declared as friend in the class exforsys as:<br><br> friend int compute (exforsys e1) <br><br><br><br><br><br><br><br>disadvantage of friend functions is that they require an extra line<br>of code when you want dynamic binding. To get the effect of a virtual friend,<br>the friend function should call a hidden (usually protected:) virtual[20]<br>member function. Source: CoolInterview.com

Answered by: THE DJ AK | Date: 12/2/2009 | 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. <br>It is declared within the class with a prefix " friend ".<br><br>* The keyword friend is placed only in the function declaration of the friend <br>function and not in the function definition.<br><br><br>* When a class is declared as a friend, the friend class has access to the <br>private data of the class that made this a friend.<br><br><br><br> Source: CoolInterview.com

Answered by: Satish Aware | Date: 1/17/2010 | Contact Satish Aware Contact Satish Aware

friend function can access the private data member of a single or more than two classes. It has no restriction while using the private member of the classes. With the help of friend function we can share the private data of two classes. A friend function can be declared in the public or private area. Source: CoolInterview.com

Answered by: Pradeep Kumar | Date: 4/26/2010 | Contact Pradeep Kumar Contact Pradeep Kumar

consider two classes class1 and class2...now i ask one questn....<br>will u acces a private data member of class1 by member function of class2 without use of INHERITANCE....<br>is this possible?????? no???<br> But C++ answers s for that by providing a new concept as frnd function which is frnd function for class1 and class2 and we can easily acces private data member of classes.. Source: CoolInterview.com

Answered by: Prakash | Date: 6/27/2010 | Contact Prakash Contact Prakash

COMPLETE EXPLANATION:
A friend function of a class is not a member function of the class,but a friend function has access to the private members.
A friend function of a class is an ordinary function except that it access to the private members of objects of that class.For this we just made a function friend by using a keyword "friend" before the function declaration.
EX:
class sample{
int a,b;
public:
void value()
{
a=10;
b=20;
}
friend float mean(sample);
};

float mean(sample obj)
{
return((obj.a+obj.b)/2);
}
void main()
{
sample obj1;
obj1.setvalue();
cout<<"Mean="<<Mean(obj1);
getch();
} Source: CoolInterview.com

Answered by: Kiran Kala | Date: 7/29/2010 | Contact Kiran Kala Contact Kiran Kala

A friend function is a special type of function which allows direct access to the data members and member functions which may be either private or public of any class remaining outside of the class. It not a member function but has full access to the class. Source: CoolInterview.com

Answered by: Narayan Joshi | Date: 8/11/2010 | Contact Narayan Joshi Contact Narayan Joshi


If you have the better answer, then send it to us. We will display your answer after the approval

Rules to Post Answers in CoolInterview.com:-

  • There should not be any Spelling Mistakes.
  • There should not be any Gramatical Errors.
  • Answers must not contain any bad words.
  • Answers should not be the repeat of same answer, already approved.
  • Answer should be complete in itself.

Post your answer here

Inform me about updated answers to this question.
Related Questions
View Answer
What is a template?


View Answer
How can you force instantiation of a template?

View Answer
Can we have "Virtual Constructors"?


- Templates in C++ Interview Questions & Answers" class="btn btn-primary btn-xs">View Answer
How do you implement thread synchronization (Object.Wait, Notify,and CriticalSection) in C#?
View Answer
What is a template?
View Answer
What is namespace?
View Answer

Please Note: We keep on updating better answers to this site. In case you are looking for Jobs, Pls Click Here Vyoms.com - Best Freshers & Experienced Jobs Website.

View All Templates in C++ Interview Questions & Answers - Exam Mode / Learning Mode




India News Network
Latest 20 Questions
Payment of time- barred debt is: (a) Valid (b) Void (c) Illegal (d) Voidable
Consideration is defined in the Indian Contract Act,1872 in: (a) Section 2(f) (b) Section 2(e) (c) Section 2(g) (d) Section 2(d)
Which of the following is not an exception to the rule, "No consideration, No contract": (a) Natural love and affection (b) Compensation for involuntary services (c) Completed gift (d) Agency
Consideration must move at the desire of: (a) The promisor (b) The promisee (c) The promisor or any other party (d) Both the promisor and the promisee
An offer which is open for acceptance over a period of time is: (a) Cross Offer (b) Counter Offer (c) Standing Offer (d) Implied Offer
Specific offer can be communicated to__________ (a) All the parties of contract (b) General public in universe (c) Specific person (d) None of the above
_________ amounts to rejection of the original offer. (a) Cross offer (b) Special offer (c) Standing offer (d) Counter offer
A advertises to sell his old car by advertising in a newspaper. This offer is caleed: (a) General Offer (b) Special Offer (c) Continuing Offer (d) None of the above
In case a counter offer is made, the original offer stands: (a) Rejected (b) Accepted automatically (c) Accepted subject to certain modifications and variations (d) None of the above
In case of unenforceable contract having some technical defect, parties (a) Can sue upon it (b) Cannot sue upon it (c) Should consider it to be illegal (d) None of the above
If entire specified goods is perished before entering into contract of sale, the contract is (a) Valid (b) Void (c) Voidable (d) Cancelled
______________ contracts are also caled contracts with executed consideration. (a) Unilateral (b) Completed (c) Bilateral (d) Executory
A offers B to supply books @ Rs 100 each but B accepts the same with condition of 10% discount. This is a case of (a) Counter Offer (b) Cross Offer (c) Specific Offer (d) General Offer
_____________ is a game of chance. (a) Conditional Contract (b) Contingent Contract (c) Wagering Contract (d) Quasi Contract
There is no binding contract in case of _______ as one's offer cannot be constructed as acceptance (a) Cross Offer (b) Standing Offer (c) Counter Offer (d) Special Offer
An offer is made with an intention to have negotiation from other party. This type of offer is: (a) Invitation to offer (b) Valid offer (c) Voidable (d) None of the above
When an offer is made to the world at large, it is ____________ offer. (a) Counter (b) Special (c) General (d) None of the above
Implied contract even if not in writing or express words is perfectly _______________ if all the conditions are satisfied:- (a) Void (b) Voidable (c) Valid (d) Illegal
A specific offer can be accepted by ___________. (a) Any person (b) Any friend to offeror (c) The person to whom it is made (d) Any friend of offeree
An agreement toput a fire on a person's car is a ______: (a) Legal (b) Voidable (c) Valid (d) Illegal
Cache = 0.125 Seconds