What is friend function?

Answers were Sorted based on User's Feedback



What is friend function?..

Answer / niranjan ambati

If you want to explicitly grant access to a function that
isn’t a member of the current class. It allows external
functions to access the private members of a class. You can
declare a global function as a friend, and you can also
declare a member function of another class, or even an
entire class, as a friend.
The compiler ignores the access level (public, private,
protected) where friend functions are declared. Friend
functions are inherently public.
Friend function should be used as minimum as possible, but
it can be used safely in the following way.. by calling the
same class object where friend function exists.. if you
want to interact with other class then call that class
objects in friends function which interacts the private
members of the class.
class NA
{
int i;
public:
CA(int i):i(t){}
friend void fun(NA *);
};
void fun(NA *obj)
{
cout <<obj->i<<endl;
}
void main()
{
NA obj(22);
fun(&obj);
};

Is This Answer Correct ?    9 Yes 11 No

What is friend function?..

Answer / niranjan ambati

If you want to explicitly grant access to a function that
isn’t a member of the current class then friend functions
can be useful. It allows external functions to access the
private members of a class. You can declare a global
function as a friend, and you can also declare a member
function of another class, or even an entire class, as a
friend.
The compiler ignores the access level (public, private,
protected) where friend functions are declared. Friend
functions are inherently public.
Friend function should be used as minimum as possible, but
it can be used safely in the following way.. by calling the
same class object where friend function exists.. if you
want to interact with other class then call that class
objects in friends function which interacts the private
members of the class.
class NA
{
int i;
public:
NA(int i):i(t){}
friend void fun(NA *);
};
void fun(NA *obj)
{
cout <<obj->i<<endl;
}
void main()
{
NA obj(22);
fun(&obj);
};

Is This Answer Correct ?    5 Yes 7 No

Post New Answer

More OOPS Interview Questions

Write an operator overloading program to write S3+=S2.

2 Answers  


In OverLoading concept,Why they are not consider return value and why they are consider only parameters in method? For ex: public int Add(int a,int b){...} public String Add(int a,int b){...}

1 Answers  


How compiler selects(internally) required overridden function in inheritance?

2 Answers   CSC, Infinite Computer Solutions,


what are the realtime excercises in C++?

0 Answers   IBM, Wipro,


What are the 4 pillars of oop?

0 Answers  






How to deploy web appliction in web logic ?

1 Answers   Unisys,


Iam doing my project on instant messaging , if you any new ideas regarding this project ,please suggest it?

2 Answers  


What is protected in oop?

0 Answers  


What is the purpose of polymorphism?

0 Answers  


Why a "operator=(...)" when there is a copy ctor?

2 Answers  


Tell me the scenario,Where we can use interfaces or Abstract class.And What is the difference between interfaces and abstract class?

5 Answers  


what is abstract class ? when is used in real time ? give a exp

5 Answers  


Categories