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 is a superclass in oop?
What is persistence in oop?
Whats is abstraction in oops?
Why is polymorphism used?
What are different oops concepts?
What is polymorphism give a real life example?
What is inheritance in oop?
What is object-oriented programming? Webopedia definition
Plese get me a perfect C++ program for railway/airway reservation with all details.
What is difference between pop and oop?
Can a varargs method be overloaded?
What is multilevel inheritance?
What is the difference between encapsulation and polymorphism?
Describe these concepts: Polymorphism, Inheritance and Abstraction.
What is polymorphism and why is it important?