comp.lang.ada
 help / color / mirror / Atom feed
From: "Matthew Heaney" <mheaney@on2.com>
Subject: Re: OO in Ada
Date: Fri, 4 Oct 2002 11:05:26 -0400
Date: 2002-10-04T11:05:26-04:00	[thread overview]
Message-ID: <uprbhnca8ls274@corp.supernews.com> (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...
>
> Suppose I have defined an object Pen to have the form:
>
> +-------------------------------------------+
> | Pen                                       |
> +-------------------------------------------+
> | X     : Integer;                          |
> | Y     : Integer;                          |
> | Color : Color_Type;                       |
> +-------------------------------------------+
> | procedure Move_To(This : in out Pen;      |
> |                   X    : in     Natural;  |
> |                   Y    : in     Natural); |
> | -- move the pen to the specified location |
> | --  without drawing                       |
> |                                           |
> | procedure Draw_To(This : in out Pen;      |
> |                   X    : in     Natural;  |
> |                   Y    : in     Natural); |
> | -- draw a line from the current location  |
> | --  to (X,Y) using the pen's              |
> | --  current color.  the pen's location    |
> | --  moves to (X,Y)                        |
> |                                           |
> | function New_Pen(X     : Natural;         |
> |                  Y     : Natural;         |
> |                  Color : Color_Type)      |
> | return Pen_Access;                        |
> | -- returns access to an initialised Pen   |
> +-------------------------------------------+

I'm not sure why you're using a factory function to return a pointer to a
pen object, but in case that's really what you wanted then you'd have
something like:

package Pens is

   type Pen_Type (<>) is tagged limited private;

   type Pen_Access is access all Pen_Type;

   function New_Pen (X, Y : Natural; C : Color_Type) return Pen_Access;

   procedure Move_To
     (Pen : in out Pen_Type;
      X, Y : Natural);

private

   type Pen_Type is tagged limited record
      X, Y : Integer;
      C : Color_Type;
   end record;

end Pens;



> Extension of this class to Thick_Pen should, according to me, result in a
> class having the form:
>
> +-------------------------------------------+
> | Thick_Pen                                 |
> +-------------------------------------------+
> | X         : Integer;                      |
> | Y         : Integer;                      |
> | Color     : Color_Type;                   |
> | Thickness : Positive;                     |
> +-------------------------------------------+
> | --=== inherited from Pen_Class ===--      |
> | procedure Move_To(This : in out Pen;      |
> |                   X    : in     Natural;  |
> |                   Y    : in     Natural); |
> | -- move the pen to the specified location |
> | --  without drawing                       |
> |                                           |
> | procedure Draw_To(This : in out Pen;      |
> |                   X    : in     Natural;  |
> |                   Y    : in     Natural); |
> | -- draw a line from the current location  |
> | --  to (X,Y) using the pen's              |
> | --  current color.  the pen's location    |
> | --  moves to (X,Y)                        |
> |                                           |
> | function New_Pen(X     : Natural;         |
> |                  Y     : Natural;         |
> |                  Color : Color_Type)      |
> | return Pen_Access;                        |
> | -- returns access to an initialised Pen   |
> |                                           |
> | --=== new in Thick_Pen_Class ===--        |
> | procedure Move_To(This : in out Thick_Pen;|
> |                   X    : in     Natural;  |
> |                   Y    : in     Natural); |
> | -- move the pen to the specified location |
> | --  without drawing                       |
> |                                           |
> | procedure Draw_To(This : in out Thick_Pen;|
> |                   X    : in Natural;      |
> |                   Y    : in Natural);     |
> | -- draw a line from the current location  |
> | --  to (X,Y) using the pen's              |
> | -- current color.  the pen's location     |
> | --  moves to (X,Y).  The                  |
> | -- vertical thickness of the pen is given |
> | --  by Thickness.                         |
> |                                           |
> | function New_Thick_Pen                    |
> |    (X         : Natural;                  |
> |     Y         : Natural;                  |
> |     Color     : Color_Type;               |
> |     Thickness : in Positive)              |
> | return Thick_Pen_Access;                  |
> | -- returns access to initialised Thick_Pen|
> |-------------------------------------------+

In general, the tagged types in a class should be declared in a hierachy of
packages:

package Pens.Thick is

   type Pen_Type is new Pens.Pen_Type with private;

   type Pen_Access is access Pen_Type;

   function New_Pen
     (X, Y : Natural;
      C : Color_Type;
      T : Positive) return Pen_Access;

   procedure Move_To
     (Pen : in out Pen_Type;
      X, Y : Natural);

private

   type Pen_Type is new Pens.Pen_Type with record
      Thickness : Positive;
   end record;

end Pens.Thick;



> 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_.

Visibility in Ada is determined by the relationship of modules, not types.
If you want a derived type to have visibility to the parent from which it is
derived, then you need to declare the derived type in a child package.
(Note that sometimes you don't want to declare the type in a child package,
but this is not one of those times.)

> 2.   To provide the derived class with access to the object attributes of
> the base class I have to create user-defined methods in the base class.

No.  Because the type is declared in a child package, then it has visibility
to the private part of the parent package.

> 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?

Indeed, you do labour.  All you need to do is declare Thick_Pen in a child
package.

> 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.

In C++, I can do this:

class C { ... };
bool operator=(const C& lhs, const C& rhs);

Why doesn't C++ use the "intuitive syntax"?








  parent reply	other threads:[~2002-10-04 15:05 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
2002-10-04  5:32 ` Simon Wright
2002-10-04  6:01 ` tmoran
2002-10-04 15:05 ` Matthew Heaney [this message]
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