title: How does a function access private data member of a class object?
body: <p>My code:-</p>
  
  <pre><code>#include&lt;iostream&gt;
  using namespace std;
  
  class a{
     private:
        int x;
     public:
        a(int data)
        {
           x=data;
        }
        friend void printPrivateMember(a);
  };
  
  void printPrivateMember(a obj)
  {
     cout&lt;&lt;obj.x; //I can access private data member by an object inside this function.
  }
  
  int main()
  {
     a obj1(5);
     printPrivateMember(obj1);
     cout&lt;&lt;obj1.x; //this gives error
     return 0;
  }
  </code></pre>
  
  <p>I wanted to know as to how can I access a PRIVATE data type by an object in the friend function but cannot do so in main.</p>
  
  <p>When I read about access specifier . It was specified that private can be accessed by only member functions (I don't have a problem with the friend function) and not by the object of that class. I wanted to know as to what difference is there because of which I can access private member by an object in one case and cannot do so in another. The same is applicable for copy constructor. </p>