title: Copy Constructor
body: <p>I am new to <code>C++ programming</code>, I got a doubt when I was reading a <code>C++</code> regarding copy constructor. Why copy constructor will call when we pass object of class to a outside function as pass by value. Please go through my code as below.</p>
  
  <pre><code>#include "stdafx.h"
  #include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  using namespace std;
  
  class Line
  {
      public:
        int getLength( void );
        Line( int len );             // simple constructor
        Line( const Line &amp;obj);      // copy constructor
        ~Line();                     // destructor
  
      private:
        int *ptr;
  };
  
  // Member functions definitions including constructor
  Line::Line(int len)
  {
      cout &lt;&lt; "Normal constructor allocating ptr" &lt;&lt; endl;
      ptr = new int;
      *ptr = len;
  }
  
  Line::Line(const Line &amp;obj)
  {
     cout &lt;&lt; "Copy constructor allocating ptr." &lt;&lt; endl;
     ptr = new int;
    *ptr = *obj.ptr; // copy the value
  }
  
  Line::~Line(void)
  {
     cout &lt;&lt; "Freeing memory!" &lt;&lt; endl;
     delete ptr;
  }
  
  int Line::getLength( void )
  {
     return *ptr;
  }
  
  void display(Line obj)//here function receiving object as pass by value 
  {
    cout &lt;&lt; "Length of line : " &lt;&lt; obj.getLength() &lt;&lt;endl;
  }
  
  // Main function for the program
  int main( )
  {
      Line line(10);
      display(line);//here i am calling outside function
     _getch();
     return 0;
  }
  </code></pre>
  
  <p>In the above the I am passing object of class as parameter and display function receiving it as pass by value. My doubt is when I pass object to a function which is not a member of class why copy constructor is calling. if I receive object as reference in <code>display()</code> function[i.e display(Line &amp;Obj)] it is not calling the copy constructor. Please help me what is the difference.</p>