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 inheritance in oop?
Whats oop mean?
what is the sylabus for priliminaries?
Whats is abstraction in oops?
Will I be able to get a picture in D drive to the c++ program? If so, help me out?
What is inheritance and how many types of inheritance?
What is difference between data abstraction and encapsulation?
Why is polymorphism needed?
c++ program to swap the objects of two different classes
class type to basic type conversion
What is coupling in oop?
What does and I oop and sksksk mean?
String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?
Which method cannot be overridden?
How do you answer polymorphism?