Eiffel

Contributed by Chris Rathman

Try Shapes (TryMe.e)

class TRYME inherit ANY
creation make

feature
   make is
      local
         i: INTEGER
         ashape: SHAPE
         scribble: ARRAY[SHAPE]
         arect: RECTANGLE

      do
         -- create some shape instances
         !!scribble.make(0, 1)
         !RECTANGLE!ashape.make_rectangle(10, 20, 5, 6)
         scribble.put(ashape, 0)
         !CIRCLE!ashape.make_circle(15, 25, 8)
         scribble.put(ashape, 1)

         -- iterate through the list and handle shapes polymorphically
         from
            i := scribble.lower
         until
            i > scribble.upper
         loop
            scribble.item(i).draw
            scribble.item(i).rmoveto(100, 100)
            scribble.item(i).draw
            i := i + 1
         end

         -- call a rectangle specific function
         !!arect.make_rectangle(0, 0, 15, 15)
         arect.setwidth(30)
         arect.draw
      end
end

Shape Class (Shape.e)

deferred class SHAPE

feature
   x: INTEGER;
   y: INTEGER;

   -- constructor
   make_shape(newx: INTEGER; newy: INTEGER) is
      do
         moveto(newx, newy)
      end

   -- accessors for x & y
   getx: INTEGER is do Result := x end
   gety: INTEGER is do Result := y end
   setx(newx: INTEGER) is do x := newx end
   sety(newy: INTEGER) is do y := newy end

   -- move the x & y position
   moveto(newx: INTEGER; newy: INTEGER) is
      do
         setx(newx)
         sety(newy)
      end
   rmoveto(deltax: INTEGER; deltay: INTEGER) is
      do
         moveto(getx + deltax, gety + deltay)
      end

   -- abstract draw method
   draw is deferred end
end

Rectangle Class (Rectangle.e)

class RECTANGLE inherit SHAPE
creation make_rectangle

feature
   width: INTEGER;
   height: INTEGER;

   -- constructor
   make_rectangle(newx: INTEGER; newy: INTEGER;
      newwidth: INTEGER; newheight: INTEGER) is
      do
         make_shape(newx, newy)
         setwidth(newwidth)
         setheight(newheight)
      end

   -- accessors for the width & height
   getwidth: INTEGER is do Result := width end
   getheight: INTEGER is do Result := height end
   setwidth(newwidth: INTEGER) is do width := newwidth end
   setheight(newheight: INTEGER) is do height := newheight end

   -- draw the rectangle
   draw is
      do
         io.put_string("Drawing a Rectangle at:(")
         io.put_integer(x)
         io.put_string(",")
         io.put_integer(y)
         io.put_string("), width ")
         io.put_integer(width)
         io.put_string(", height ")
         io.put_integer(height)
         io.put_string("%N")
      end
end

Circle Class (Circle.e)

class CIRCLE inherit SHAPE
creation make_circle

feature
   radius: INTEGER;

   -- constructor
   make_circle(newx: INTEGER; newy: INTEGER; newradius: INTEGER) is
      do
         make_shape(newx, newy)
         setradius(newradius)
      end

   -- accessors for the radius
   getradius: INTEGER is do Result := radius end
   setradius(newradius: INTEGER) is do radius := newradius end

   -- draw the circle
   draw is
      do
         io.put_string("Drawing a Circle at:(")
         io.put_integer(x)
         io.put_string(",")
         io.put_integer(y)
         io.put_string("), radius ")
         io.put_integer(radius)
         io.put_string("%N")
      end
end

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