Back to slide -- Keyboard shortcut: 'u'                      friends/situation-6/friends-sit6.cc - A function f get access to the private state of both class A and B.Lecture 4 - slide 33 : 40
Program 1

// A function f is a friend of both class A and B.
// f is outside both A and B. f can - symmetrically - access private members in both A and B.

#include <iostream>
#include <string>

class B; 

class A{
private:
  double a;

public:
  friend double f(A &aObj, B &bObj); 

  A(double a):a(a){}

  void ma();

  void print(){
    std::cout << a << std::endl;
  }
};

class B{
private:
  double b;

public:
  friend double f(A &aObj, B &bObj); 

  B(double b):b(b){}

  void mb();

  void print(){
    std::cout << b << std::endl;
  }
};

void A::ma(){
    a += 3.0;
}

void B::mb(){
    b += 4.0;
}

double f(A &aObj, B &bObj){
  return aObj.a + bObj.b;
} 

int main(){
  A aObj(1);
  B bObj(2);

  std::cout << f(aObj,bObj) << std::endl;  // 3
 
  aObj.print();  // 1
  bObj.print();  // 2
}