Sometime you want to give other classes access to private and protected methods and attributes.

You can do that by giving another class "friend" privilege.

It does not matter where you declare the friend, as long as its in the class definition.

Friends are explicit. They are NOT transitive and do NOT inherit.

Friends are often used for private constructors.

In this case we do not want to make the default constructor public, since it does not initialize all attributes.

But if we know that another class is "good" enough to set all the attributes we can make that class a friend.

class A {
  friend class B;
  int attr;
  A(int a) { attr = a; };
  void setAttr(int b) { attr = b; };

class B {
  A* attr;
  B() {
    attr = new A();

"Friends" are especially useful in the Factory design pattern. You will learn about that in a software engineering class.