Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is how you do it:</p> <pre><code>Point px; Point py; Point pz; Triangle trig(px, py, pz); </code></pre> <p><code>trig</code> will be the object, which is an instance of class <code>Triangle</code> and the above would call the constructor with 3 Point arguments.</p> <p>Another way is for pointers:</p> <pre><code> Triangle *pTrig = new Triangle(pX, pY, pZ); </code></pre> <p>In addition, I suggest, that this would be better:</p> <pre><code>Triangle::Triangle() : A(NULL), B(NULL), C(NULL) { } Triangle::Triangle(const Point&amp; X,const Point&amp; Y, const Point&amp; Z) : A(new Point(X)), B(new Point(Y)), C(new Point(Z)) { } </code></pre> <p>Assuming that Point has a copy constructor.</p> <p>You want to call it from inside the <code>operator&gt;&gt;</code> function to update argument T, but this won't work, because you cannot call the constructor on something that's already constructed. Instead, what you need is to implement an assignment operator. Please see <a href="http://en.wikipedia.org/wiki/Assignment_operator_%28C%2B%2B%29" rel="nofollow">http://en.wikipedia.org/wiki/Assignment_operator_%28C%2B%2B%29</a> for more information.</p> <p>Then you can do <code>T = Triangle(X,Y,Z);</code></p> <p>To implement the assignment operator, you can do this:</p> <pre><code>Triangle&amp; Triangle::operator= (const Triangle&amp; other) { if (this != &amp;other) // protect against invalid self-assignment { if (A != NULL) delete A; if (B != NULL) delete B; if (C != NULL) delete C; A = new Point(other.A); B = new Point(other.B); C = new Point(other.C); } return *this; } </code></pre> <p>Assuming Point has copy constructors. To implement copy constructors, please see <a href="http://en.wikipedia.org/wiki/Copy_constructor" rel="nofollow">http://en.wikipedia.org/wiki/Copy_constructor</a></p> <p>A copy constructor looks like the following, but you need to do it for Point:</p> <pre><code>Triangle&amp; Triangle::Triangle(const Triangle&amp; other) : A(new Point(other.A)), B(new Point(other.B)), C(new Point(other.C)) { } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload