Difference between member functions,non member functions and friend functions ?
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: C++ Programing
Page 1 of 1 • Share
Difference between member functions,non member functions and friend functions ?
difference between member functions,non member functions and friend functions with examples
sharmila farooqi- Monstars
- Posts : 35
Join date : 2011-05-29
Re: Difference between member functions,non member functions and friend functions ?
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) .
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
Vuhelper- Deep Bench
- Posts : 97
Join date : 2011-05-29
Similar topics
» Asalm O Alikum i am new member
» My Friend when I think of you
» Twitter looking to friend Facebook
» تمام دوستوں کے لئے ایک تحفہ ہےA Gift for All Friend
» Motorway police official kills friend in Karachi
» My Friend when I think of you
» Twitter looking to friend Facebook
» تمام دوستوں کے لئے ایک تحفہ ہےA Gift for All Friend
» Motorway police official kills friend in Karachi
Virtual Position Forum :: Members Lounge :: Sciences & Knowledge :: Programing and Design :: C++ Programing
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
Yesterday at 12:21 pm by ali001
» Hemangiom'App
Tue Nov 05, 2024 11:25 am by ali001
» MindfulMe - Mental Health App
Mon Nov 04, 2024 10:50 am by ali001
» Learn Candlestick Patterns
Tue Oct 15, 2024 5:51 am by ali001
» Woh Pagal Si Episode 52 to 62 - Top Pakistani Drama
Sat Sep 21, 2024 6:26 pm by Mir Emmad Ali Khan Domki
» Nearu - share your socials
Sat Sep 21, 2024 1:12 pm by ali001
» Nightclub Tycoon: Idle Empire
Thu Sep 19, 2024 9:16 pm by ali001
» Carnivore - Meat Diet Recipes
Wed Sep 18, 2024 2:37 pm by ali001
» Eid Milad un Nabi Mubarak 2024 (Rabiʻ I 14, 1446 AH)
Tue Sep 17, 2024 3:44 pm by Mir Emmad Ali Khan Domki