Computer Science, asked by mukesh5510, 10 months ago

what is a friend function ? what are the merits and demarits of using friend functions?​

Answers

Answered by RanaMuneeb
0

Answer:

A friend function of a class is defined outside that class' scope but it has the right to access all private and protected members of the class.

A friend can be a function, function template, or member function, or a class or class template, in which case the entire class and all of its members are friends.

Merits of friend function are:

  1. It acts as the bridge between two classes by operating on their private data’s.
  2. It can access members without the need for inheriting the class.
  3. It can be used to increase the versatility of overloading operator.
  4. It provides functions that need data which isn’t normally used by the class.
  5. Allows sharing private class information by a non-member function.

Demerits of friend function are:

  1. It violates the law of data hiding by allowing access to private members of the class from outside the class.
  2. Breach of data integrity.
  3. Conceptually messy
  4. Runtime polymorphism in the member cannot be done.
  5. Size of memory occupied by objects will be maximum.
Similar questions