title: Violation of encapsulation?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/12390408/class-data-encapsulationprivate-data-in-operator-overloading">Class Data Encapsulation(private data) in operator overloading</a>  </p>
  </blockquote>
  
  
  
  <p>Please look at this example.</p>
  
  <pre><code>class myClass {
      int a;
  public :
      myClass () {
       this-&gt;a = 0;
    }
  
    myClass(int val) {
       this-&gt;a = val;
    }
  
    void add(myClass &amp;obj2) {
       cout &lt;&lt; "Result = " &lt;&lt; this-&gt;a + obj2.a;
       obj2.a = 0;
    }
  
    void show() {
       cout &lt;&lt; "a = " &lt;&lt; this-&gt;a;
    }
  };
  
  int main() {
    myClass obj1(10), obj2(20);
  
    obj2.show(); //prints 20.
    obj1.add(obj2);
    obj2.show(); //prints 0. 
  
    return 0;
  }
  </code></pre>
  
  <p>In the add() function, I am able to access the value of a private member of obj2 when I've actually called add() in the context of obj1. Isn't this a violation of encapsulation? </p>
  
  <p>At first I was thinking that the compiler will throw me an error, but it didn't. </p>