what is a friend function ? what are the merits and demarits of using friend functions?
Answers
Answered by
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:
- It acts as the bridge between two classes by operating on their private data’s.
- It can access members without the need for inheriting the class.
- It can be used to increase the versatility of overloading operator.
- It provides functions that need data which isn’t normally used by the class.
- Allows sharing private class information by a non-member function.
Demerits of friend function are:
- It violates the law of data hiding by allowing access to private members of the class from outside the class.
- Breach of data integrity.
- Conceptually messy
- Runtime polymorphism in the member cannot be done.
- Size of memory occupied by objects will be maximum.
Similar questions
Math,
5 months ago
Social Sciences,
5 months ago
Chemistry,
11 months ago
Math,
11 months ago
Social Sciences,
1 year ago
English,
1 year ago