What is friend function?

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


Please Help Members By Posting Answers For Below Questions

What is a superclass in oop?

673


What is persistence in oop?

677


Whats is abstraction in oops?

595


Why is polymorphism used?

588


What are different oops concepts?

580






What is polymorphism give a real life example?

567


What is inheritance in oop?

607


What is object-oriented programming? Webopedia definition

729


Plese get me a perfect C++ program for railway/airway reservation with all details.

3433


What is difference between pop and oop?

611


Can a varargs method be overloaded?

620


What is multilevel inheritance?

727


What is the difference between encapsulation and polymorphism?

598


Describe these concepts: Polymorphism, Inheritance and Abstraction.

616


What is polymorphism and why is it important?

562