Re: Classwide Parameter?

From: Martin Krischik (krischik_at_users.sourceforge.net)
Date: 10/12/04


Date: Tue, 12 Oct 2004 10:10:37 +0200

matthias_k wrote:

> Thanks for that answer. However, I'm still having problems (I have
> rewritten the code to be in one single package now):
>
> <snip>
> package Graphics is
>
> type Shape is abstract tagged null record;
> procedure Draw (Obj: Shape'Class);

You want an abstract method not a class wide procedure:

procedure Draw (Obj: Shape) is abstract;

>
> type Circle is new Shape with record
> Radius: Float;
> Center: Float;
> end record;
> procedure Draw (Obj: Circle);
>
> type Square is new Shape with record
> Size: Float;
> end record;
> procedure Draw (Obj: Square);
>
> end;
> </snip>
>
> Now, what I want is to have different implementations of the Draw method
> for each Subtype. However, if I run this program:
>
> <snip>
> with Graphics;
> use Graphics;
>
> procedure Demo is
> type Reference is access all Shape'Class;
> Object: Reference;
> begin
> Object := new Circle;
> Draw( Object.all );
>
> Object := new Square;
> Draw( Object.all );

Only use pointer in Ada if you realy need to:

Test_1:
declare
     Object: Shape'Class := Circle'(Radius => 10.0, Center => 10.0);
begin
   Draw( Object);
end

Test_2:
declare
     Object: Shape'Class := Square'(Size => 10.0);
begin
   Draw( Object);
end

> end;

> </snip>
>
> The Shape's Draw method is always called here but it shouldn't. No late
> binding happens. I have tried to make it abstract, but it didn't even
> compile then. What's wrong?
>
> - Matthias

-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com


Relevant Pages

  • Re: Has working with raster graphics always been this complicated?
    ... I'm not new to programming, but I'm new to Java, at least as far as ... I find references to Image objects, Graphics ... You can draw primitives on ... those transparent pixels. ...
    (comp.lang.java.help)
  • Re: Ignored advice, am now in serious poo on graphics
    ... Then I put in a thread.sleep, after the draw in the Paint method, and noticed a strange thing - the image was shown for 1000 milliseconds, then disappears. ... If you're doing your own drawing, there's nothing in a PictureBox control that is useful to you. ... It would take a lot more than a single newsgroup post to address all of the complexities of graphics under Windows. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: problems with flips/rotations of certain shapes
    ... Always draw as if you were drawing to an untransformed graphics device. ... > With some shapes, such as a curve, they 'walk' across the screen as ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: can one convert drawable XmlSerializer xmlns file into GIF or PNG?
    ... >>> metafile with commands used to draw images. ... >> is there a method to draw the graphics? ... >> mentions a Draw routine which takes a Graphics object as a parameter. ... I suggest you read up on XML first. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Linux gForth/VFX windows
    ... horizontal nib (3 pixels) ... Draw a circle segment. ... These are hardware coordinates. ... In graphics mode EMIT, TYPE etcetera still work, but now they ...
    (comp.lang.forth)