title: How to provide protection to friend function in C++
body: <p>I have just started learning friend functions in C++.This is the program which I am using for the concept exploration.</p>
  
  <pre><code>#include&lt;iostream&gt;
  
  using namespace std;
  
  class one
  {
  private:
      int age;
  public:
      one()
      {
          age=1;
      }
      void setData(int num)
      {
          age=num;
      }
  friend int returnOne(one a);
  };
  int returnOne(one a)
  {
      return a.age;
  }
  
  
  class two
  {
  private:
      int roll;
  public:
      two()
      {
          roll=0;
      }
      void setData(int num)
      {
          roll=num;
      }
  friend int returnTwo(two b);
  };
  
  int returnTwo(two b)
  {
      return b.roll;
  }
  
  
  int main()
  {
      one a;
      two b;
      a.setData(10);
      b.setData(12);
      cout&lt;&lt;returnOne(a)&lt;&lt;endl&lt;&lt;returnTwo(b)&lt;&lt;endl;
  } 
  </code></pre>
  
  <p>Now I am worried that the security of class <code>one</code> and <code>two</code> has been compromised as now anyone can use those globally defined friend functions to access class <code>one</code>'s and class <code>two</code>'s private members.
  How can I provide protection to these friend functions or restrict their usage?</p>