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
What is overriding vs overloading?
write knight tour problem which is present in datastructure
What is inheritance in oop?
What is the important feature of inheritance?
What is abstraction in oop?
What is protected in oop?
What does oop mean in snapchat?
How do you use inheritance in unity?
what is graphics
What are the data types in oop?
What does no cap mean?
Advantage and disadvantage of routing in telecom sector
write a program using c++ to implement single contiguous memory mangement techniques.display the content of the main memory after yhe allocation of jobs and percentage of the wastage of the main memory
What is polymorphism and its types?
What is persistence in oop?