Simula

Contributed by Chris Rathman

Simula - Shape Example

begin

   class Shape(x, y); integer x; integer y;
   virtual: procedure draw;
   begin
      comment -- get the x & y components for the object --;
      integer procedure getX;
         getX := x;
      integer procedure getY;
         getY := y;

      comment -- set the x & y coordinates for the object --;
      integer procedure setX(newx); integer newx;
         x := newx;
      integer procedure setY(newy); integer newy;
         y := newy;

      comment -- move the x & y position of the object --;
      procedure moveTo(newx, newy); integer newx; integer newy;
         begin
            setX(newx);
            setY(newy);
         end moveTo;
      procedure rMoveTo(deltax, deltay); integer deltax; integer deltay;
         begin
            setX(deltax + getX);
            setY(deltay + getY);
         end moveTo;
   end Shape;

   Shape class Rectangle(width, height); integer width; integer height;
   begin
      comment -- get the width & height of the object --;
      integer procedure getWidth;
         getWidth := width;
      integer procedure getHeight;
         getHeight := height;

      comment -- set the width & height of the object --;
      integer procedure setWidth(newwidth); integer newwidth;
         width := newwidth;
      integer procedure setHeight(newheight); integer newheight;
         height := newheight;

      comment -- draw the rectangle --;
      procedure draw;
         begin
            Outtext("Drawing a Rectangle at:(");
            Outint(getX, 0);
            Outtext(",");
            Outint(getY, 0);
            Outtext("), width ");
            Outint(getWidth, 0);
            Outtext(", height ");
            Outint(getHeight, 0);
            Outimage;
         end draw;
   end Circle;

   Shape class Circle(radius); integer radius;
   begin
      comment -- get the radius of the object --;
      integer procedure getRadius;
         getRadius := radius;

      comment -- set the radius of the object --;
      integer procedure setRadius(newradius); integer newradius;
         radius := newradius;

      comment -- draw the circle --;
      procedure draw;
         begin
            Outtext("Drawing a Circle at:(");
            Outint(getX, 0);
            Outtext(",");
            Outint(getY, 0);
            Outtext("), radius ");
            Outint(getRadius, 0);
            Outimage;
         end draw;
   end Circle;

   comment -- declare the variables used --;
   ref(Shape) array scribble(1:2);
   ref(Rectangle) arectangle;
   integer i;

   comment -- populate the array with various shape instances --;
   scribble(1) :- new Rectangle(10, 20, 5, 6);
   scribble(2) :- new Circle(15, 25, 8);

   comment -- iterate through the list and handle shapes polymorphically --;
   for i := 1 step 1 until 2 do
      begin
         scribble(i).draw;
         scribble(i).rMoveTo(100, 100);
         scribble(i).draw;
      end;

   comment -- call a rectangle specific instance --;
   arectangle :- new Rectangle(0, 0, 15, 15);
   arectangle.draw;
   arectangle.setWidth(30);
   arectangle.draw;
end

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