Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position


Join the forum, it's quick and easy

Virtual Position Forum
Please register to watch content in detail
Thanks
Admin virtual position
Virtual Position Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Difference between member functions,non member functions and friend functions ?

View previous topic View next topic Go down

GMT + 3 Hours Difference between member functions,non member functions and friend functions ?

Post by sharmila farooqi Mon May 30, 2011 5:31 am

difference between member functions,non member functions and friend functions with examples
avatar
sharmila farooqi
Monstars
Monstars

Posts : 35
Join date : 2011-05-29

Back to top Go down

GMT + 3 Hours Re: Difference between member functions,non member functions and friend functions ?

Post by Vuhelper Mon May 30, 2011 5:32 am

In C++, we know that private members cannot be accessed from the outside class.
That is a non-member function cannot have an access to the private data of a class.
However there could be a situation where we would like two classes to share a particular function.
For example consider a case where two classes, manager and scientist have been defined.
We would like to use a function income_ tax () to operate on the objects of both these classes.
In such situation, C++ allows the common function to be made friendly with both the classes. Such a function needs not be member of any these classes.
To make outside function friendly to a class, we have to simply declare this function as a friend of a class as shown below:
Class ABC
{
……
…..
Public:
……..
……..
Friend void xyz (void); //declaration
};

When the function is logically coupled with the class (like your maze connectedness example)
When the function needs to access private or protected members, it's better to make it a member than a friend. when it's a generic function that can be templatized to naturally work on other classes (look at the header for good example) .
avatar
Vuhelper
Deep Bench
Deep Bench

Posts : 97
Join date : 2011-05-29

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

Permissions in this forum:
You cannot reply to topics in this forum