Note that there are some explanatory texts on larger screens.

plurals
  1. POGet Struct from child class and use in main class
    primarykey
    data
    text
    <p>Here is the issue with my code</p> <p>I got a parent class <code>Shape</code> and a child class <code>Square</code></p> <pre><code>struct Point { int x,y; }; class Shape { private: string name; public: void setName(string); string getName(); virtual void setVerticlePoint() { }; virtual Point getVerticlePoint(); }; </code></pre> <p>Below is Square.cpp</p> <pre><code>class Square: public Shape { private: Point verticlePoint[4]; public: int noOfVerticles() const { return 5; } void setVerticlePoint(); Point getVerticlePoint(); }; void Square::setVerticlePoint() { int xData,yData; for(int i=0; i&lt;4; i++) { cout &lt;&lt; "Please enter x-ordinate of pt." &lt;&lt; i &lt;&lt; " :"; cin &gt; xData; cout &lt;&lt; "Please enter y-ordinate of pt." &lt;&lt; i &lt;&lt; " :"; cin &gt;&gt; yData; verticlePoint[i]-&gt;x = xData; verticlePoint[i]-&gt;y = yData; } } </code></pre> <p>So at main.cpp , i did this</p> <pre><code>int main() { Shape *point[5]; Point vertic`lePoint; Square *cro = new Square(); // some codes in between // now i prompt for verticle point shape[0] = squr; //assuming myShape &amp; myType is some string value shape[0]-&gt;setName(myShape); shape[0].setType(myType); //here i will be prompt to key in 4 times of X &amp; y Ordinate of the verticle point shape[0]-&gt;setVerticlePoint(); //Now the issue is i need to retrieve back my verticle point that i store in shape[0]. //here no issue cout &lt;&lt; "Get Name: " &lt;&lt; shape[0]-&gt;getName() &lt;&lt; endl; cout &lt;&lt; "Get Data:" &lt;&lt; shape[0]-&gt;getType() &lt;&lt; endl; //the top 2 works //this is the issue verticlePoint = shape[0]-&gt;getVerticlePoint(); cout &lt;&lt; sizeof(verticlePoint); //it output as size 8 , no matter which shape i use, i got other shapes with different verticle point. return 0; } </code></pre> <p>Issue is :</p> <p>How do i retrieve the <code>verticlePoint[array]</code> (e.g for square is <code>verticlePoint[4]</code>, how do i retrieve the Point into a variable in main class, and then use a for loop to call out the <code>x,y</code> of <code>shape[0]</code> for <code>verticlePoint[0]</code> to <code>verticlePoint[4]</code></p> <p>Thanks for all help!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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