C++

Contributed by Chris Rathman

Shape Class (Shape.h)

class Shape {

public:
   Shape(int newx, int newy);
   int getX();
   int getY();
   void setX(int newx);
   void setY(int newy);
   void moveTo(int newx, int newy);
   void rMoveTo(int deltax, int deltay);
   virtual void draw();

private:
   int x;
   int y;
};

Shape Implementation (Shape.cpp)

#include "Shape.h"

// constructor
Shape::Shape(int newx, int newy) {
   moveTo(newx, newy);
}

// accessors for x & y
int Shape::getX() { return x; }
int Shape::getY() { return y; }
void Shape::setX(int newx) { x = newx; }
void Shape::setY(int newy) { y = newy; }

// move the shape position
void Shape::moveTo(int newx, int newy) {
   setX(newx);
   setY(newy);
}
void Shape::rMoveTo(int deltax, int deltay) {
   moveTo(getX() + deltax, getY() + deltay);
}

// abstract draw method
void Shape::draw() {
}

Rectangle Class (Rectangle.h)

class Rectangle: public Shape {

public:
   Rectangle(int newx, int newy, int newwidth, int newheight);
   int getWidth();
   int getHeight();
   void setWidth(int newwidth);
   void setHeight(int newheight);
   void draw();

private:
   int width;
   int height;
};

Rectangle Implementation (Rectangle.cpp)

#include "Shape.h"
#include "Rectangle.h"
#include <iostream.h>

// constructor
Rectangle::Rectangle(int newx, int newy, int newwidth, int newheight): Shape(newx, newy) {
   setWidth(newwidth);
   setHeight(newheight);
}

// accessors for width and height
int Rectangle::getWidth() { return width; }
int Rectangle::getHeight() { return height; }
void Rectangle::setWidth(int newwidth) { width = newwidth; }
void Rectangle::setHeight(int newheight) { height = newheight; }

// draw the rectangle
void Rectangle::draw() {
   cout << "Drawing a Rectangle at:(" << getX() << "," << getY() <<
      "), width " << getWidth() << ", height " << getHeight() << endl;
}

Circle Interface (Circle.h)

class Circle: public Shape {

public:
   Circle(int newx, int newy, int newradius);
   int getRadius();
   void setRadius(int newradius);
   void draw();

private:
   int radius;
};

Circle Implementation (Circle.cpp)

#include "Shape.h"
#include "Circle.h"
#include <iostream.h>

// constructor
Circle::Circle(int newx, int newy, int newradius): Shape(newx, newy) {
   setRadius(newradius);
}

// accessors for the radius
int Circle::getRadius() { return radius; }
void Circle::setRadius(int newradius) { radius = newradius; }

// draw the circle
void Circle::draw() {
  cout << "Drawing a Circle at:(" << getX() << "," << getY() <<
      "), radius " << getRadius() << endl;
}

Try shapes module (Polymorph.cpp)

#include <iostream.h>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"

int main(void) {
   // set up array to the shapes
   Shape *scribble[2];
   scribble[0] = new Rectangle(10, 20, 5, 6);
   scribble[1] = new Circle(15, 25, 8);

   // iterate through the array and handle shapes polymorphically
   for (int i = 0; i < 2; i++) {
      scribble[i]->draw();
      scribble[i]->rMoveTo(100, 100);
      scribble[i]->draw();
   }

   // call a rectangle specific function
   Rectangle *arec = new Rectangle(0, 0, 15, 15);
   arec->setWidth(30);
   arec->draw();
}

Output

Drawing a Rectangle at:(10,20), width 5, height 6
Drawing a Rectangle at:(110,120), width 5, height 6
Drawing a Circle at:(15,25), radius 8
Drawing a Circle at:(115,125), radius 8
Drawing a Rectangle at:(0,0), width 30, height 15

Chris Rathman / Chris.Rathman@tx.rr.com