PDA

View Full Version : Friend Class and Function in C++



ankitdixit
23rd March 2020, 08:25
Hello Everyone, I am new in this community and I am confused to friend class and function in c++ programming. Someone asked me this question why we need this functionality in c++? and I have no answer at that time. I have researched it but still, I am confused. Can anyone here be c++ expert who explain to me why we need this? or suggest me some related question which is useful for my upcoming interview.

d_stranz
23rd March 2020, 17:58
In most cases, good programming style says you should not use friend classes or functions. The entire concept of object-oriented programming languages (like C++ or Python) is that the world should see your classes only through their API (in other words, their public methods). This means you can change the implementation of the class internally, but if the external API stays the same, users of the class will not see and changes.

Friend classes are sometimes useful if you have two classes that tightly cooperate with each other. One class might serve as the public interface for a set of features, but there might be another class that really does all of the work. In that case, you might make the second class a "friend" of the first, so the second class could update variables or call protected or private methods in the first class.

Friend classes are also sometimes used when deriving a new class by inheriting the first one is not appropriate. For example, a second class might contain a member variable for an instance of the first class, and needs to call protected functions in order to update it. Deriving from the first class is not a good solution, because the second class might not obey the "is-a" rule for object hierarchies. (A derived class must be - "is-a" - class that can be treated as if it was the base class. A coin and a bill are both "money").

An instance where a friend class might be useful:

You have an electronic cash gift card. You can use an card kiosk to read the value stored in the card, but you can't change the value. In an API, this means that the Card:: getValue() method is public, but the Card:: setValue() method is not. However, you can use another kiosk to deposit cash and have it credited to the card. In this case, the Kiosk class might be declared as a "friend" of the Card class, and so the Kiosk class would be able to call the Card:: setValue() method to update the value of the card.

Deriving Kiosk from Card does not make sense, because a Kiosk "is not a" Card.

Hope this helps. No doubt, Google could give you a much better explanation. Try "use case for C++ friend class". Lots of useful hits.

khatrivijaysingh1994
31st March 2020, 13:15
Hello Everyone, I am new in this community and I am confused to friend class and function in c++ programming. Someone asked me this question why we need this functionality in c++? and I have no answer at that time. I have researched it but still, I am confused. Can anyone here be c++ expert who explains to me why we need this? or suggest me some related question which is useful for my upcoming interview.

Friend Function is function used with a friend keyword to grant a non-member function access to the private members of a class and friend class is a class used with a friend keyword to access the private members of another class.

A friend's function can be used in some situations of operator overloading whereas A friend class can be used when a class is created on the top of another class.

Here (https://hackr.io/blog/cpp-interview-questions) you can find a complete list of interview questions.