Sponsored Links

Results 1 to 2 of 2

Thread: difference between member functions,non member functions and friend functions ?

  1. #1
    Senior Member saneha's Avatar
    Join Date
    Apr 2011

    11 difference between member functions,non member functions and friend functions ?

    Sponsored Links1

  2. #2
    Administrator Vuhelper's Avatar
    Join Date
    Apr 2011
    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
    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 <algorithms> header for good example) .

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Is void used only with functions? If yes than why?
    By Vuhelper in forum C++ Programing
    Replies: 0
    Last Post: 11-05-2011, 05:10 PM
  2. Replies: 0
    Last Post: 07-20-2011, 10:45 PM
  3. Replies: 1
    Last Post: 05-02-2011, 06:48 AM
  4. What are constant members functions in c++ 2011
    By saneha in forum C++ Programing
    Replies: 1
    Last Post: 04-29-2011, 08:18 AM
  5. Replies: 1
    Last Post: 04-24-2011, 05:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
-: Vuhelp Disclaimer :-
None of the files shown here are hosted or transmitted by this server. The links are provided solely by this site's users. The administrator's or staff of Vuhelp.net cannot be held responsible for what its users post, or any other actions of its users. You may not use this site to distribute or download any material when you do not have the legal rights to do so. It is your own responsibility to adhere to these terms. If you have any doubts about legality of content or you have any suspicions, feel free to contact us.
Online Education | JhelumSoft