answersLogoWhite

0


Best Answer

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) .

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Predefined (or standard library) functions are those functions which are already defined like the printf() and the scanf(), whereas the userdefined functions are the functions those are created by us in the program and will be called later and used.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

A friend function is declared outside of the class in which it is declared a friend but has the same privileges as any other member of the class. Moreover, a friend function can be a friend to more than one class, and can also be a member of another class altogether. The only exception is that class constructors and destructors cannot be declared friends, since they are not functions and cannot be called directly.

Since friends have the same access rights as member functions, they can also be considered members of the class in which they are declared a friend. The only difference is that friend functions are not scoped to the class.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

A member function (or member method) is a function that is declared to be a method of a class, such as a class accessor (getter), mutator (setter), or operation.

A friend function is a highly-privileged, external function or a member method of another class, that is permitted fullaccess to all the private and protected members of the class in which it is declared.

Friend functions should always be treated with caution because of their highly-privileged status. There are often alternatives to using friend functions, but when one class works closely with another and requires access to some or all of its hidden members it may be the only way to allow the two to work together. Many will claim this breaks the rules of encapsulation (data hiding) but this is frankly nonsense. Used appropriately, they can effectively extend a class interface, making it all the more useful. There's nothing worse than a class that is made difficult to use because of some non-existent "rule" that states you cannot not use them. Use them if you must, but use them wisely.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

Non-member functions have access to public member functions only, unless they are declared friends of the class, in which case they also have access to the private and protected members of the class.

This answer is:
User Avatar

User Avatar

Wiki User

7y ago

A normal or ordinary function is any function declared outside of any class definition. A member function is a function declared inside a class definition; it is a member of the class in which it is declared and therefore has private access to the class representation. Non-static member functions are scoped to an object of the class and have an implicit this pointer which refers to that object. Static member functions are scoped to the class as a whole and do not require any instances. Non-member functions that require private access may be declared friends of the class; a friend function may be an ordinary function or a member of another class.

Non-static member functions have the following three properties:

  1. Private access to the class representation.
  2. Scoped to the class.
  3. Must be invoked against an instance of the class (have a this pointer).

Static member functions have the first two properties only while friend functions have the first property only. Functions that are neither members of the class nor friends of the class have none of these properties.

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

friend function is used to access the attributes in private class, where as the normal member function cant access the private data unless it is declared within the same class.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How does friend operator function differ from member operator function?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.

Related questions

Why a friend function cannot be used to overload the assignment operator?

Assignment(=) operator is a special operator that will be provided by the constructor to the class when programmer has not provided(overloaded) as member of the class.(like copy constructor). When programmer is overloading = operator using friend function, two = operations will exists: 1) compiler is providing = operator 2) programmer is providing(overloading) = operator by friend function. Then simply ambiguity will be created and compiler will gives error. Its compilation error.


What is the difference between friend function and normal member function?

We can access a Friend function from any other class in which friend function is introduced or declared even if the other class is not a member of first class. But when we use normal member function, we can have its access only in the derived classes of the first class. This is the basic difference between a friend function and a normal member function.


Explain the advantages and disadvantages of friend functions?

by: THE DJ AKwww.the-dj-ak.webs.comwww.thedjak.co.nrwww.thedjak.webs.comWhat is a Friend Function?A friend function is a special function in c++ which inspite of not being member fuctionof a class has privalage to access private and protected data of a class.A friend function is a non member function of a class, that is declared as a friend usingthe keyword "friend" inside the class. By declaring a function as a friend, all the accesspermissions are given to the function.A friend function is used for accessing the non-public members of a class.A class can allow non-member functions and other classes to access its ownprivate data, by making them friends. Thus, a friend function is an ordinaryfunction or a member of another class.Need for Friend Function:As discussed in the earlier sections on access specifiers, when a datais declared as private inside a class, then it is not accessible from outsidethe class. A function that is not a member or an external class will notbe able to access the private data. A programmer may have a situation wherehe or she would need to access private data from non-member functions andexternal classes. For handling such cases, the concept of Friend functionsis a useful tool.How to define and use Friend Function in C++:The friend function is written as any other normal function, exceptthe function declaration of these functions is preceded with the keywordfriend. The friend function must have the class to which it is declared asfriend passed to it in argument.Some important points to note while using friend functions in C++:* The keyword friend is placed only in the function declaration of the friendfunction and not in the function definition..* It is possible to declare a function as friend in any number of classes..* When a class is declared as a friend, the friend class has access to theprivate data of the class that made this a friend..* A friend function, even though it is not a member function, would have therights to access the private members of the class..* It is possible to declare the friend function as either private or public..* The function can be invoked without the use of an object. The friend functionhas its argument as objects, seen in example below.properties of friend function:1. if a function to be made friend of a class than it should be declared within bodyof the class priciding with keyword friend.2.freind function never breaks the security.3.it should not be defined in name of class nor scope resolution operator is used in it'sdefination even the keyword freind is also not used while defining friend function.4.when friend function is called nither name of object nor dot operator is used. howeverit may accept the object as argument who's value it want's to access.5.it doen't matter in which section of the class we have declared a freind function.Example to understand the friend function:#includeclass exforsys{private:int a,b;public:void test(){a=100;b=200;}friend int compute(exforsys e1)//Friend Function Declaration with keyword friend and with the object of class exforsys to which it is friend passedto it};int compute(exforsys e1){//Friend Function Definition which has access to private datareturn int(e1.a+e2.b)-5;}main(){exforsys e;e.test();cout


How do you differentiate between a member function and normal function?

A normal function is any function that is not a member of any class. Normal functions that operate upon a class are referred to as non-member functions, however a non-member function can also be a member of another class. Any class may declare any non-member function to be a friend of the class, in which case the function becomes a friend function.A member function is a member of a class and may be declared static or non-static. Non-static member functions have the following 3 properties:Private access to the class members.Scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. Non-member functions that are not friends of the class have none of these properties.


Why we make a function friend in c plus plus?

A "normal" function is just a function. Even a friend function is just a normal function. However, when a class declares an external function or an external class method to be a friend of the class, the friend function gains access to the private members of the class. class foo { friend void bar(foo&); int m_data; }; void bar(foo& f) { f.m_data=42; } In the example above, the foo class declares the bar function to be a friend function. As such, the bar function has unrestricted access to the private members of foo. In this case, foo::m_data is private (by default) and would therefore be inaccessible to bar were it not declared a friend of foo. Other than that, the bar function is no different to any other function. Note that you cannot declare friendship from outside of a class. The class itself must declare its own friends. However, the same function can be declared friends in more than one class, which can be a useful feature when two or more classes work closely together, as the friend function can be used to provide the "glue" that binds them together.


When is a friend function compulsory?

A normal member function has the following qualities: 1. Has private access to the class. 2. Is scoped to the class. 3. Can be invoked on an instance of the class. Static functions have the first two qualities only. Friend functions have the first quality only. It therefore follows that friend functions are only compulsory when you need quality 1 only.


Is it possible to restrict a friend class's access to the private data member?

No. De-friend the friend class and provide an access method function.


Why friend are used in object oriented programing?

A friend function is a function that cannot be declared a member of a class but which requires private access to that class. For example, a function that operates upon two different classes cannot be a member of both classes, but if the function requires private access to both classes then it has to be a friend to at least one of them.To fully appreciate friend functions, consider that a non-static member function has the following three properties:Has private access to the class.Is scoped to the class.Must be invoked against an object of the class (has a 'this' pointer).Static member functions have the first two properties only while friend functions have the first property only. All other non-member functions have none of these properties.


Why ostream operators not overloaded using member functions?

Consider the following line: cout<<obj; where obj is the object of Demo class. In this case we are overloading "<<" operator. But overloading the binary operator using member function, the left hand operand should be the object of relevant class. Here in this case left hand side operand is not the object of Demo class. It is object of ostream class. Hence we cant overload ostream operators using member function. But we can overload these type of operators using friend functions. Thanks, Prof. D. H. Ingole


How would you access data members of a class in cases inside member function of another class?

Either make the data members public, or make the member function a friend of the class containing the data member.


What is friendship in c plus plus?

Whenever you declare a member function, that function has three properties: 1. The function can access the private representation of the class. 2. The function is in the scope of the class. 3. The function must be invoked on an object (it has a this pointer). When you declare a static member function, that function has the first two properties only. When you declare a nonmember function to be a friend of the class, that function has the first property only. When you first begin designing classes it is often tempting to declare every function that uses the class to be a member of that class. However, it is important to keep class interfaces as clean and as simple as possible. Aside from the class constructors and its destructor, certain functions must always be declared members, such as assignment and compound assignment operators, conversion operators, setters and getters. But all other functions must be carefully considered. For instance, overloading the + operator as a class member is not necessary if the class already has a public copy or move constructor and a public += operator. It can simply be declared a nonmember function: foo operator+ (foo a, const foo& b) { return a += b; } Note that argument a is passed by value (and is therefore copied or moved automatically) while argument b is a constant reference. The + operator is implemented in terms of the public += operator, returning the result by value (which is again copied or moved automatically). Declaring this operator as a member function offers no advantages whatsoever, it would simply pollute the foo namespace unnecessarily. In effect, the + operator overload becomes part of the global namespace -- which is exactly where it belongs because it does not require any of the three properties listed at the beginning of this answer. Therefore, when deciding upon a function's membership, always ask which of the three properties it requires. If the answer is none, then it is a nonmember function. Friendship comes into play when a nonmember function requires the first property only. This could be because the public interface is tightly constrained (as it should be) and modifying it to suit just one nonmember function would be considered undesirable (exposing far more than it really needs to). However, the most common reasons for friendship are because the function is already member of another class (no function can be a member of more than one class) or where the function requires private access to two or more classes. In some cases, two separate classes might form a unified unit (such as parent and child classes) where each needs private access to the other. So long as the first property is the only requirement, friendship is the preferred option. There are some who believe friendship undermines encapsulation, however this is errant nonsense. Although a friend is not a member of the class that declares it to be a friend, it is still considered part of the class interface, just as the nonmember + operator is considered part of the foo interface. The only difference is that friends require access to the private representation of the class. As such, they must obey the encapsulation rules and maintain any and all invariants of the class, just as any member function must. You are in control of the design and implementation of both the class and its friends. The only way you can undermine encapsulation is by allowing friend access to a function you have no control over.


Why do you member functions defined outside the class?

A member function of a class can be defined outside the class using scope resolution :: operator Thanks Rajneesh