title: C++ Passing an Object to a Member Function
body: <p>For a homework assignment we are asked to create two classes: a MyPoint class and a ThreeDPoint class. </p>
  
  <p>Here is my MyPoint class:</p>
  
  <pre><code>#pragma once 
  #include &lt;math.h&gt;
  
  class MyPoint
  {
  private:
      double x, y;
  
  public:
      MyPoint()
      {
          x = y = 0;
      }
  
      MyPoint(double x, double y)
      {
          this-&gt;x = x;
          this-&gt;y = y;
      }
  
      double getX() 
      {
          return x;
      }
  
      double getY() 
      {
          return y;
      }
  
      double distance(MyPoint pointB)
      {
          return sqrt((x - pointB.x) * (x - pointB.x) 
              + (y - pointB.y) * (y - pointB.y));
      }
  };
  </code></pre>
  
  <p>Here is my ThreeDPoint class:</p>
  
  <pre><code>#pragma once
  #include "MyPoint.h"
  
  class ThreeDPoint : public MyPoint
  {
  private:
      double z;
  
  public:
      ThreeDPoint() // : MyPoint()
      {
          z = 0;
      }
  
      ThreeDPoint(double x, double y, double z) : MyPoint(x, y)
      {
          this-&gt;z = z;
      }
  
      double getZ()
      {
          return z;
      }
  
      double distance(ThreeDPoint pointB) // function overloading
      {
          // x and y are private not protected
          return sqrt((getX() - pointB.getX()) * (getX() - pointB.getX()) 
              + (getY() - pointB.getY()) * (getY() - pointB.getY()) 
              + (getZ() - pointB.getZ()) * (getZ() - pointB.getZ()));
      }
  };
  </code></pre>
  
  <p>And here is main:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "SignatureBlock.h"
  #include "MyPoint.h"
  #include "ThreeDPoint.h"
  
  int main()
  {
      SignatureBlock myBlock;
      std::cout &lt;&lt; myBlock.toString();
  
      MyPoint pointA(1, 2);
      MyPoint pointB(4, 2.5);
  
      std::cout &lt;&lt; pointA.distance(pointB) &lt;&lt; '\n';
  
      ThreeDPoint point_a(0, 0, 0);
      ThreeDPoint point_b(10, 30, 25.5);
  
      std::cout &lt;&lt; point_a.distance(point_b) &lt;&lt; '\n';
  
      return 0;
  }
  </code></pre>
  
  <p>This code works fine. It is able to calculate the distance between two points in both 2-dimensional and 3-dimensional space. However, in the homework instructions for the ThreeDPoint class, the book says to create </p>
  
  <p>"A constant get function that returns the z value" </p>
  
  <p>and</p>
  
  <p>"A constant <code>distance(const MyPoint&amp;)</code> function to return the distance between this point and the other point in three-dimensional space." </p>
  
  <p>My question is what do they mean by "A constant get function..." and "A constant distance(const MyPoint&amp;) function..."? I understand that you can put <code>const</code> after a function declaration to prevent the function from changing any class members, but why is this necessary here?</p>
  
  <p>Also, why would I pass <code>const MyPoint&amp;</code> to the distance function rather than a ThreeDPoint object? After all we're trying to find the distance between two ThreeDPoints. I read that if you have a parent object parameter in the member function declaration any child of that parent can also be passed to the function, so maybe that has something to do with it. </p>
  
  <p>Lastly, isn't the <code>distance(const MyPoint&amp;)</code> in the instructions missing a name for the MyPoint&amp; object? I did try to type this in and the compiler doesn't mind, but how would I access the members of the object being passed in? </p>
  
  <p>Edit: Also, what purpose does the &amp; serve?</p>
  
  <p>Most importantly, how would I re write my code to follow exactly what the instructions state?</p>
  
  <p>Thanks for reading and thank you for any suggestions anyone might have. </p>