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

Why is polymorphism important in oop?

0 Answers  


What is the different between Applet and Application?

2 Answers  


What language is oop?

0 Answers  


What is polymorphism used for?

0 Answers  


What does and I oop mean in text?

0 Answers  






What is the difference between abstraction and polymorphism?

0 Answers  


What do we mean by a hidden argument in C++?

1 Answers  


What is encapsulation process?

0 Answers  


What is late bound function call and early bound function call? Differentiate.

2 Answers   Ness Technologies,


What is a linked list?

7 Answers   IBM, Neural Info,


sir i want to know which posting to apply since i am BE CSE.. also want to know what are the rounds there for my interview...Expecting for ur valuable answer....

2 Answers  


what is a ststic variable and stiticfunction briefly explain with exmple and in which case we use

2 Answers   HCL,


Categories