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

What is Method overloading?

5 Answers  


write a c++ program to find maximum of two numbers using inline functions.

3 Answers   IBM,


to find out the minimum of two integer number of two different classes using friend function

0 Answers  


oops concept is used for?

3 Answers   Synergy,


why function overloading is not called as pure polymorphism?

2 Answers  






polymorphism means?

6 Answers   BFL,


officer say me - i am offered to a smoking , then what can you say

0 Answers  


What is encapsulation oop?

0 Answers  


Program to check whether a word starts with a capital letter or not.

1 Answers   Infosys,


c++ is a purely oop concept?

3 Answers   Google, TCS,


what is the technical or oop name of object?

1 Answers  


Write a program to sort the number with different sorts in one program ??

0 Answers   NIIT,


Categories