comp.lang.ada
 help / color / mirror / Atom feed
From: "Chad R. Meiners" <crmeiners@hotmail.com>
Subject: Re: OO in Ada
Date: Thu, 3 Oct 2002 23:37:09 -0400
Date: 2002-10-03T23:37:09-04:00	[thread overview]
Message-ID: <anj2g2$16ri$1@msunews.cl.msu.edu> (raw)
In-Reply-To: mailman.1033697703.29112.comp.lang.ada@ada.eu.org


"Rick Duley" <rickduley@hotmail.com> wrote in message
news:mailman.1033697703.29112.comp.lang.ada@ada.eu.org...
> Hi all
>> and there is no-one I know in Perth to talk to.  I'm hoping you folk
won't
> mind giving me a hand.

Always willing to help an academic in need ;)

I believe the following chunk of code is a proper method of representing the
pens in an OO manner.  Let me know if this doesn't address your concerns.

package Pens is

   type Colors is (Red, Green, Blue);

   type Pen is tagged private;

   procedure Move (Item : in out Pen; To_X : Natural; To_Y : Natural);

   procedure Draw (Item : in out Pen; To_X : Natural; To_Y : Natural);

   package Builder is
      -- The builder is not a primitive of Pen'class
      function New_Pen (Color : Colors; X : Natural; Y : Natural) return
Pen;

   end Builder;

private

   type Pen is tagged record
      Color : Colors;
      X     : Natural;
      Y     : Natural;
   end record;

end Pens;

package Pens.Thick is

   type Thick_Pen is new Pen with private;
   -- This type and its procedures and function have full access to
   -- Pen's representation.

   procedure Draw (Item : in out Thick_Pen; To_X : Natural; To_Y : Natural);

   package Builder is

      function New_Thick_Pen (Color     : Colors;
                              X         : Natural;
                              Y         : Natural;
                              Thickness : Positive )
                             return Thick_Pen;

   end Builder;

private

   type Thick_Pen is new Pen with record
      Thickness : Positive;
   end record;

end Pens.Thick;

with Pens.Thick;

use  Pens, Pens.Builder, Pens.Thick, Pens.Thick.Builder;

procedure Test_Pens is

   procedure Draw_Box (Item : in out Pen'Class; X1 : Natural; Y1 : Natural;
                                                X2 : Natural; Y2 : Natural)
is

      -- Example of dispatching.

   begin
      Move (Item, X1, Y1);

      Draw (Item, X2, Y1);
      Draw (Item, X2, Y2);
      Draw (Item, X1, Y2);
      Draw (Item, X1, Y1);
   end Draw_Box;

   Green_Pen  : Pens.Pen := New_Pen (Green, 3, 4);
   Red_Pen    : Pens.Thick.Thick_Pen := New_Thick_Pen(Red, 3, 4, 1);

begin

   Draw_Box (Green_Pen, 0, 0, 50, 50);
   Draw_Box (Red_Pen, 10, 10, 30, 30);

end Test_Pens;

> 1.   All the texts I can find which deal in any degree at all with OO in
Ada
> teach that the tagged record in an object declaration should be 'private'.
> When you follow this line, a derived class does not have direct access to
> the object attributes of the base class, i.e. _there_is_no_inheritace_.

If you need access to a base classes representation, create the derived
class in a child package.

> 3.   If the tagged record in the base class is left public and the derived
> class is in a child package of the package defining the base class, then
the
> base class attributes are accessible to the derived class.  So far so good
> :)  However, if in a program 'use'ing the child package of 'Thick_Pen'
(and
> not mentioning the base package of 'Pen') I make a call to the routine
> 'Draw' with an actual parameter of the type 'Pen' I get a compiler error
> message to the effect that 'Draw' is not visible.  In other words,
Thick_Pen
> has not inherited the operation Draw for 'Pen'  Again,
> _there_is_no_inheritace_.

Please post a code example with the compiler error with these types of
questions.  It will help us help you.

> I have to say that this is the first time in pretty near a decade I have
> been writing in and teaching with Ada that Ada hasn't come up with the
> goods.  Do I labour under some serious misunderstanding, do I have
something
> terribly wrong?

hmm... Well Ada's method of OOP takes a little time to get use to, but it
sounds like you have some misunderstandings of the OO system.

> 5.   One final thing (for this time anyway ;), why is it that that Ada
does
> not use the intuitive 'object.method' syntax for making calls to and
object.
>    This would mean that (in the case described in section 3) the call
would
> read
>
> Pen.Draw(To_X => n, To_Y => n);
>
> and with inheritance this would be accessible through the child package
> declaring Thick_Pen.  While I'm okay with using the existing syntax, I
feel
> the 'object.method' syntax is more intuitive and in line with OO thinking.
> If there are people working on Ada0x then maybe we should be putting this
> forward for consideration.

The object.method syntax is inconsistent with the rest of Ada's procedure
and function syntax.  I don't think the 'object.method' syntax is more
intuitive since I have seen too many TA's try to 'help' struggling students
by saying "OOP is when you use the 'object.method' syntax" ;)  I prefer
Ada's method of OOP although I don't use it much.

-CRM





  parent reply	other threads:[~2002-10-04  3:37 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-10-04  2:14 OO in Ada Rick Duley
2002-10-04  2:55 ` Jim Rogers
2002-10-04 17:35   ` Hyman Rosen
2002-10-05  0:20     ` Jim Rogers
2002-10-05 23:38       ` Dmitry A.Kazakov
2002-10-05 15:25         ` Jim Rogers
2002-10-06 21:37           ` Dmitry A.Kazakov
2002-10-06  2:18       ` Hyman Rosen
2002-10-06  3:00         ` Jim Rogers
2002-10-08 21:08           ` Gisle Sælensminde
2002-10-04  3:37 ` Chad R. Meiners [this message]
2002-10-04  5:32 ` Simon Wright
2002-10-04  6:01 ` tmoran
2002-10-04 15:05 ` Matthew Heaney
2002-10-05  2:14 ` SteveD
2002-10-05  8:54   ` Preben Randhol
2002-10-07 14:10   ` Matthew Heaney
2002-10-07 19:52     ` Jeffrey Carter
2002-10-08 21:18     ` Dmitry A.Kazakov
2002-10-08  9:53 ` John McCabe
2002-10-08 15:37   ` Matthew Heaney
2002-10-08 16:47     ` Georg Bauhaus
2002-10-08 17:48       ` Matthew Heaney
2002-10-08 17:16     ` Warren W. Gay VE3WWG
2002-10-08 17:58       ` Matthew Heaney
2002-10-09 16:59         ` Warren W. Gay VE3WWG
2002-10-08 10:21 ` Preben Randhol
  -- strict thread matches above, loose matches on Subject: below --
1998-05-15  0:00 Gisle S{lensminde
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox