05-02-2011, 04:56 PM
difference between member functions,non member functions and friend functions with examples
|
difference between member functions,non member functions and friend functions ?
|
|
05-02-2011, 04:56 PM
difference between member functions,non member functions and friend functions with examples
05-02-2011, 04:57 PM
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 <algorithms> header for good example) . 0300-9520262 Get solution in 24 hour |cpmputer & internet mileys81:[SIZE="4"][SIZE="3"]....[/SIZE][/SIZE]
|
|
« Next Oldest | Next Newest »
|