Results 1 to 3 of 3

Thread: Friend Class and Function in C++

  1. #1

    Default Friend Class and Function in C++

    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.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Friend Class and Function in C++

    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Mar 2020
    Posts
    2
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Friend Class and Function in C++

    Quote Originally Posted by ankitdixit View Post
    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 you can find a complete list of interview questions.

Similar Threads

  1. friend class - or what else to do?
    By davidlamhauge in forum Qt Programming
    Replies: 4
    Last Post: 4th May 2012, 13:11
  2. compiler error in calling friend function!!??
    By aurora in forum Qt Programming
    Replies: 11
    Last Post: 15th November 2011, 22:25
  3. friend function in QT
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2009, 15:59
  4. friend class in TC++ book
    By mickey in forum General Programming
    Replies: 11
    Last Post: 25th August 2008, 23:08
  5. QTextBrowser and friend function
    By probine in forum Qt Programming
    Replies: 4
    Last Post: 14th December 2006, 18:03

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.