title: Polymorphism & Derived Classes
body: <p>So here is my code: </p>
  
  <pre><code>//Shapes.cpp
  #include &lt;cassert&gt;
  #include &lt;cmath&gt;
  #include "shapes.h"
  
  using namespace std;
  
  const double PI = 3.14159;
  
  //////////////////////////  Ellipse  //////////////////////////
  
  Ellipse::Ellipse() : xRad(0), yRad(0){}
  
  Ellipse::Ellipse(double xRad_in, double yRad_in)
    : xRad(xRad_in), yRad(yRad_in) {}
  
  double Ellipse::area() const {
    return PI * xRad * yRad;
  }
  
  void Ellipse::draw(Canvas *canvas) const{
    // Iterate through the grid of (x,y) pixel coordinates
    // in the canvas.
    for(int x = 0; x &lt; CANVAS_WIDTH; ++x){
      for(int y = 0; y &lt; CANVAS_HEIGHT; ++y){
  
        // The ellipse contains the point (x,y) if and only if
        // ((x-xPos)/xRad)^2 + ((y-yPos)/yRad)^2 &lt;= 1
  
        double xDiff = x - get_xPos();
        double yDiff = y - get_yPos();
  
        if( (xDiff/xRad)*(xDiff/xRad) + (yDiff/yRad)*(yDiff/yRad) &lt;= 1 ){
          // If the pixel is contained in the ellipse, set it to true
          canvas-&gt;setPixel(x, y, true);
        }
      }
    }
  }
  
  ///////////////////////// End Ellipse /////////////////////////
  
  
  
  //////////////////////////  Circle  //////////////////////////
  
  // PUT YOUR CODE (IMPLEMENTATIONS) FOR CIRCLE HERE
  Circle::Circle(double rad_in)
    : Ellipse(rad_in, rad_in) {}
  
  //Use Ellipse's area function by sending it the radius of the
  //circle for the xRad and yRad parameters
  
  //Use Ellipse's draw function
  
  
  ///////////////////////// End Circle /////////////////////////
  
  
  
  ////////////////////////  Rectangle  /////////////////////////
  
  // PUT YOUR CODE (IMPLEMENTATIONS) FOR RECTANGLE HERE
  Rectangle::Rectangle(double w_in, double h_in)
    : w(w_in), h(h_in) {}
  
  double Rectangle::area() const {
    return w * h;
  }
  
  void Rectangle::draw(Canvas *canvas) const{
    // Iterate through the grid of (x,y) pixel coordinates
    // in the canvas.
    for(int x = 0; x &lt; CANVAS_WIDTH; ++x){
      for(int y = 0; y &lt; CANVAS_HEIGHT; ++y){
  
        // The Rectangle contains the point (x,y) if and only if
        // ((x-xPos)/xRad)^2 + ((y-yPos)/yRad)^2 &lt;= 1
  
        double xDiff = x - get_xPos();
        double yDiff = y - get_yPos();
  
        if( abs(xDiff) &lt;= w/2 &amp;&amp; abs(yDiff) &lt;= h/2 ){
          // If the pixel is contained in the Rectangle, set it to true
          canvas-&gt;setPixel(x, y, true);
        }
      }
    }
  }
  
  //////////////////////// End Rectangle //////////////////////
  </code></pre>
  
  <p>Along with the corresponding .h file:</p>
  
  <pre><code>    // Shapes.h
  #ifndef SHAPES_H
  #define SHAPES_H
  
  #include "Canvas.h"
  
  ///////////////////////////  Shape  ///////////////////////////
  
  class Shape {
  public:
  
    //EFFECTS: creates a shape with initial position (0,0)
    Shape() : xPos(0), yPos(0) {}
  
    //EFFECTS: returns the area of this Shape
    virtual double area() const = 0;
  
    //MODIFIES: canvas
    //EFFECTS: draws this shape onto canvas at its current position
    virtual void draw(Canvas *canvas) const {}
  
    //MODIFIES: xPos, yPos
    //EFFECTS: sets the position of this shape
    void setPosition(double xPos_in, double yPos_in){
      xPos = xPos_in;
      yPos = yPos_in;
    }
  
    double get_xPos() const { return xPos; }
    double get_yPos() const { return yPos; }
  
  private:
  
    double xPos; // The x position of this shape
    double yPos; // The y position of this shape
  
  };
  
  /////////////////////////  End Shape  /////////////////////////
  
  
  
  //////////////////////////  Ellipse  //////////////////////////
  
  class Ellipse : public Shape{
  public:
  
    Ellipse();
  
    //REQUIRES: xRad_in, yRad_in are non-negative
    //EFFECTS: creates an Ellipse with given x and y radii
    Ellipse(double xRad_in, double yRad_in);
  
    //EFFECTS: returns the area of this Ellipse
    virtual double area() const;
  
    //MODIFIES: canvas
    //EFFECTS: draws this shape onto canvas
    virtual void draw(Canvas *canvas) const;
  
  private:
    double xRad;  //Half the x-axis of the ellipse
    double yRad;  //Half the y-axis of the ellipse
  
  };
  
  ///////////////////////// End Ellipse ////////////////////////
  
  
  
  ///////////////////////////////////////////////////////////////
  //               DO NOT MODIFY ABOVE THIS LINE               //
  ///////////////////////////////////////////////////////////////
  
  
  
  //////////////////////////  Circle  //////////////////////////
  
  // PUT YOUR CODE (DECLARATION) FOR CIRCLE HERE
  class Circle : public Ellipse{
  public:
  
    //REQUIRES: rad_in is non-negative
    //EFFECTS: creates an Circle with given radius
    Circle(double rad_in);
  
    //EFFECTS: returns the area of this Circle
    virtual double area() const;
  
    //MODIFIES: canvas
    //EFFECTS: draws this shape onto canvas
    virtual void draw(Canvas *canvas) const;
  
  private:
    double xRad;  //Radius of the Circle
    double yRad;  //Radius of the Circle
  
  };
  
  
  ///////////////////////// End Circle /////////////////////////
  
  
  
  ////////////////////////  Rectangle  /////////////////////////
  
  // PUT YOUR CODE (DECLARATION) FOR RECTANGLE HERE
  class Rectangle : public Shape{
  public:
  
    //REQUIRES: xRad_in, yRad_in are non-negative
    //EFFECTS: creates an Rectangle with given x and y radii
    Rectangle(double w_in, double h_in);
  
    //EFFECTS: returns the area of this Rectangle
    virtual double area() const;
  
    //MODIFIES: canvas
    //EFFECTS: draws this shape onto canvas
    virtual void draw(Canvas *canvas) const;
  
  private:
    double w;  //Length of the Rectangle
    double h;  //Width of the Rectangle
  
  };
  
  //////////////////////// End Rectangle //////////////////////
  
  
  
  
  
  #endif /* SHAPES_H */
  </code></pre>
  
  <p>I am supposed to be making Rectangle derived from Shape and Circle derived from Ellipse, both with the corresponding functions that are present in their implementations, and I thought my code had done so, but I got the following compiler error:</p>
  
  <pre><code>shapes.cpp: In constructor \u2018Circle::Circle(double)\u2019:
  shapes.cpp:47:30: error: no matching function for call to \u2018Ellipse::Ellipse()\u2019
     : xRad(rad_in), yRad(rad_in) {}
                                ^
  shapes.cpp:47:30: note: candidates are:
  shapes.cpp:12:1: note: Ellipse::Ellipse(double, double)
   Ellipse::Ellipse(double xRad_in, double yRad_in)
   ^
  shapes.cpp:12:1: note:   candidate expects 2 arguments, 0 provided
  In file included from shapes.cpp:4:0:
  shapes.h:45:7: note: Ellipse::Ellipse(const Ellipse&amp;)
   class Ellipse : public Shape{
         ^
  shapes.h:45:7: note:   candidate expects 1 argument, 0 provided
  </code></pre>
  
  <p>I really have no idea what's wrong. Please help!</p>
  
  <p>EDIT: Additional Code necessary for compile:</p>
  
  <pre><code>// Canvas.cpp    
  #include &lt;iostream&gt;
      #include &lt;cassert&gt;
      #include "Canvas.h"
  
  using namespace std;
  
  /////////////////////////  Canvas  ///////////////////////////
  
  Canvas::Canvas(){
    for(int row = 0; row &lt; CANVAS_HEIGHT; ++row){
      for(int col = 0; col &lt; CANVAS_WIDTH; ++col){
        grid[row][col] = false;
      }
    } 
  }
  
  void Canvas::setPixel(int x, int y, bool value){
    assert(0 &lt;= x); assert(x &lt; CANVAS_WIDTH);
    assert(0 &lt;= y); assert(y &lt; CANVAS_HEIGHT);
    grid[y][x] = value;
  }
  
  void Canvas::print() const {
    for(int row = 0; row &lt; CANVAS_HEIGHT; ++row){
      for(int col = 0; col &lt; CANVAS_WIDTH; ++col){
        cout &lt;&lt; (grid[CANVAS_HEIGHT-row-1][col] ? PIXEL_ON : PIXEL_OFF) &lt;&lt; " ";
      }
      cout &lt;&lt; endl;
    } 
  }
  
  ////////////////////////// End Canvas /////////////////////////
  </code></pre>
  
  <p>And Canvas.h:</p>
  
  <pre><code>#ifndef CANVAS_H
  #define CANVAS_H
  
  /////////////////////////  Canvas  ///////////////////////////
  
  //Canvas Constants
  const int CANVAS_WIDTH = 30;
  const int CANVAS_HEIGHT = 30;
  
  const char PIXEL_ON = '#';
  const char PIXEL_OFF = ' ';
  
  class Canvas {
    //OVERVIEW:  A Canvas object represents a 2D grid of "pixels"
    //           which can be set to either "on" or "off".  A Canvas
    //           knows how to print itself out to the terminal.  The
    //           canvas has a fixed width and height and the origin
    //           (0,0) of the canvas's coordinate system is at the
    //           bottom left.
  
  public:
  
    //EFFECTS: creates a new Canvas with size CANVAS_WIDTH x CANVAS_HEIGHT
    Canvas();
  
    //REQUIRES: the pixel is on the canvas (0 &lt;= x &lt; CANVAS_WIDTH, 0 &lt;= y &lt; CANVAS_HEIGHT)
    //MODIFIES: grid
    //EFFECTS: if value is true, turns the pixel at (x,y) on
    //         if value is false, turns the pixel at (x,y) off
    void setPixel(int x, int y, bool value);
  
    //EFFECTS: prints this canvas to cout
    void print() const;
  
  private:
  
    bool grid[CANVAS_HEIGHT][CANVAS_WIDTH];
  
  };
  
  ////////////////////////// End Canvas /////////////////////////
  
  
  
  #endif /* CANVAS_H */
  </code></pre>