answersLogoWhite

0


Best Answer

All class member functions have the following qualities:

1. Private access to the class members.

2. Scoped to the class.

3. Has an implicit 'this' pointer.

Static member functions only have the first two qualities while friend functions only have the first quality.

Friend functions must be defined outside of the class in which they are declared a friend -- only the function prototype is required in the friend declaration.

User Avatar

Wiki User

βˆ™ 9y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

βˆ™ 9y ago

If a non-member function requires access to a class representation where a public interface would be undesirable, you have to declare that function a friend of the class. Only the class itself can grant friend access.

All 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 on an object of the class (has a this pointer).

Static member functions only have the first two properties while friend functions only have the first property. All other functions have none of these properties.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 10y ago

Friend functions extend the class interface outside of the class. Normally, classes are self-contained entities combining data and the methods that operate upon that data. Only class members have access to the private members of the class. However, sometimes it is desirable to allow external functions to gain access to the private members of a class. This should only be done when a function cannot be considered a member of the class, but where the public interface is insufficient for the function to operate.

Friend functions are best utilised when two or more classes must work closely with one another. In a one-way friendship, one or more of the dominant class' methods will be declared as friend functions of the subservient class. In a two-way friendship, the entire dominant class will be declared a friend of the subservient class while one or more of the subservient class' methods are declared friend functions of the dominant class.

External functions that are not members of a class but that require private access to a class can also be declared friends of the class. External functions are often employed where two or more classes work quite closely together but have no friendship with each other. The external functions then act as a link between these otherwise separate classes.

Since friends are highly privileged, you should never grant friendship to a class or function that you have no control over. Allowing a third party to gain friend access to your class could easily undermine your class' objectives. But while many will claim friendship undermines encapsulation this is, frankly, errant nonsense. When you are in total control of the class and all its friends, then only you -- the programmer -- can undermine the encapsulation. Friends are effectively members of the class and they must adhere to the exact same rules that internal members of the class must adhere to. And since you wrote the rules in the first place, the onus is upon you to stick by them: the compiler cannot help you on this regard.

That said, friendship should only be granted when it is absolutely necessary to do so. That is, if the public interface is insufficient, then you can either create a public interface to suit the function (which is not always desirable), or grant friend access to the private interface.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 11y ago

A friend function is a function that is granted privileged access to the private members of the class in which it is declared a friend. In other words, it has the same access rights as a class member function.

Friends are used to extend a class interface. However, there should always be a good reason for declaring a friend. If a function does not require privileged access to private members of a class, then it has no business being a friend. When people say friends undermine encapsulation, this is only true when friend access is inappropriate. There are always alternatives to granting a friend access to private data, but when used appropriately, friends can only enhance encapsulation, never undermine it.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 9y ago

Friend functions are implemented just like any other function. The only real difference is that they have private access to the classes in which they are declared friends.

We can think of an ordinary class member function as having three basic properties:

1. It has access to the private parts of the class.

2. It is scoped to the class.

3. It is scoped to an instance of the class (has a this pointer).

A static member function only has the first two properties while a friend function only has the first property.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 10y ago

The only use of a friend function when overloading an operator (or indeed with regard to any external function) is when no interface is available that would allow the operator (or function) to perform the operation, and where enabling a suitable interface would result in undesirable exposure that would undermine the encapsulation of the class.

It is important that the friend be treated just as if it were an actual member of the class. That is, just as members of the class must enforce encapsulation from within the class, friend operators and functions must enforce encapsulation from outwith the class. This generally means that the programmer directly responsible for the class implementation must also be responsible for implementing the friend. Allowing third parties to gain control over the friend implementation will undermine encapsulation, in which case it would far less harmful to simply provide a suitable interface and prohibit the friend access altogether.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 7y ago

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:

  1. Has private access to the class.
  2. Is scoped to the class.
  3. 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.

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 15y ago

It is a function with keyword friend. it is a non member function of a class that as the access to the private and protected members of a class.hope that helps regard SID

This answer is:
User Avatar

User Avatar

Wiki User

βˆ™ 12y ago

A friend function is a function that has access to all the private and/or protected members of the class in which it is declared.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can friend function be implemented?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the transport function implemented by both TCP and UDP?

IP


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.


Are there any agent function that cannot be implemented by any agent program?

Yes there are agent functions that can not be implemented e.g. an agent can not find the largest prime number.


What is character oriented function?

What about strchr? Or, isdigit and its family (often implemented as macros, though)?


Any possible binary logic function can be implemented using only?

AND and NOT; OR and NOT; EQU and NOT; XOR


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.


Can you turn the language down in mw3?

Sadly no, they have not implemented that function yet, that is a great idea though.


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


Which is a function that a COM object's base interface provides?

A means of discovering the interfaces implemented by other components


What is the difference between friend function and inheritance in c plus plus?

There is no such thing. When declaring a friend function only the explicitly-scoped friend is granted private access. The friend function may well be declared virtual within its own class but none of its overrides are granted access unless they are explicitly granted access.


Can a friend function be friend with more than one class?

yes


Write down the function to insert an element into a queue, in which the queue is implemented as an array?

bring the police they will be in que by themselves