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 polymorphism means?
What is the purpose of polymorphism?
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
What is difference between multiple inheritance and multilevel inheritance?
Write a program to reverse a string using recursive function?
What is this pointer in oop?
What is the difference between a constructor and a destructor?
What is abstract class in oop?
What is balance factor?
class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash
What is oops?what is its use in software engineering?
What is solid in oops?
any one please tell me the purpose of operator overloading
What is the main feature of oop?
is there any choice in opting subjects like 4 out of 7