title: Polymorphism with new data members
body: <p>I would like to write a function that can initialize and return objects of different classes using polymorphism. I also would like these classes to have different data members which may be called through the virtual function. What I wrote below might work. Could you check if I have some undefined behavior in there? Thank you! One thing I am worried about is that when I call "delete polypoint" at the end it will not free the data member "scale" that is unique to "CRectangle". If my code doesn't work is there a way to make it work?</p>
  
  <pre><code>class CPolygon {
    protected:
      int width, height;
    public:
      void set_values (int a, int b)
        { width=a; height=b; }
      virtual int area ()
        { return (0); }
    };
  
  class CRectangle: public CPolygon {
    public:
      int scale;
      int area ()
        { return (width * height * scale ); }
    };
  
  CPolygon *polytestinner()
  {
      CPolygon *polypoint = 0;
  
      int consoleinput = 2;
      if (consoleinput&gt;1)
      {
          CRectangle *rectpoint = new CRectangle();
          rectpoint-&gt;scale = 4;
          polypoint = rectpoint;
      }
      polypoint-&gt;set_values(3,4);
      return polypoint;
  }
  
  void polytest()
  {
      CPolygon *polypoint = polytestinner();
      gstd::print&lt;int&gt;(polypoint-&gt;area());    
      delete polypoint;
  }
  
  int main()
  {
      polytest();
      return 0;
  }
  </code></pre>
  
  <p>Thank you!</p>