Answer Posted / 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 View All Answers
How do you answer polymorphism?
What is byval and byref? What are differences between them?
What is polymorphism what is it for and how is it used?
What are the three main types of variables?
What is oops and its features?
What is the difference between inheritance and polymorphism?
What is abstraction with example?
What do you mean by overloading?
What is abstraction in oops with example?
What are two types of polymorphism?
What is class and example?
What is multilevel inheritance explain with example?
Why is abstraction needed?
What are the types of abstraction?
What is overloading in oops?