-1
$\begingroup$

In C++ ,friend class allows us to inherit the proprieties of one class to another class(es) which is also possible using inheritance. And also in inheritance we can set different levels of privileges to the child class(es) depending on our requirements, which suggests inheritance is more useful than friend class. Then what more can friend class do which cannot be done using inheritance? Why is friend class introduced when there is already inheritance property which has even more functionalities?

$\endgroup$
  • 5
    $\begingroup$ Sounds like a question for stackoverflow. $\endgroup$ – Yuval Filmus Feb 29 '16 at 13:01
  • 2
    $\begingroup$ This isn't the place to ask about the difference in usage, but it might be reasonable to ask about the difference in meaning. Inheritance (should) denote Liskov substitutability, which is a different notion from privacy. $\endgroup$ – Pseudonym Feb 29 '16 at 13:35
  • 3
    $\begingroup$ Copy of stackoverflow.com/questions/18085169/… $\endgroup$ – Bojack Feb 29 '16 at 14:07
  • 3
    $\begingroup$ To whomever keeps flagging these comments: they are neither rude nor offensive so they won't be removed. In fact, the comments here strike me as particularly constructive, as they tell the OP 1) where the question is more likely to be well-received, 2) how this question may be refocused to be ontopic here, and 3) where an answer can be found. $\endgroup$ – Raphael Feb 29 '16 at 20:11
  • $\begingroup$ Let's say your cat has fleas. That's the cat's private business. But you, as the cat's owner, want access to that private business. Without being a cat yourself. That's the important thing: Inheritance would mean only cats can determine if one cat has fleas. "friend" classes and functions mean other classes can as well. $\endgroup$ – gnasher729 Jan 10 '17 at 10:50
3
$\begingroup$

To declare a class (or a function) a friend allows the friend to access private members [the naming in C++ is not my fault!] (data or function) directly. They aren't "inherited" in any meaningful way. Yes, it creates a interdependency, but very different and a lot weaker than inheritance does.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.