Answer Posted / 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 |
Post New Answer View All Answers
What are functions in oop?
How long to learn object oriented programming?
What is solid in oops?
Why do pointers exist?
What is class and object in oops?
What is the real life example of polymorphism?
What is balance factor?
Why it is called runtime polymorphism?
What is a superclass in oop?
State what is encapsulation and friend function?
Who invented oop?
What is polymorphism and example?
Can abstract class have normal methods?
What is an advantage of polymorphism?
class type to basic type conversion