comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <laoXhai@ix.netcom.com>
Subject: Re: Inheritance and Polymorphism in Ada !!
Date: 1999/10/15
Date: 1999-10-15T17:25:58+00:00	[thread overview]
Message-ID: <7u7o36$tv8$1@nntp6.atl.mindspring.net> (raw)
In-Reply-To: 3806DC34.1513E8B1@frqnet.de

In article <3806DC34.1513E8B1@frqnet.de>,
	Andreas Winckler <andreas.winckler@frqnet.de> wrote:

>
>Chango Cho schrieb:
>> 
>>     Do they exist in Ada?
>
>Yes, but in my opinion polymorphism is kind of restricted in Ada.

There are lots of alternatives to dynamic polymorphism in Ada.  Here is
a simple package with three simplified examples.

package Thingamabobs is                              --  1
  type Gadget is tagged private;                     --  2
  type Gadget_Pointer is access all Gadget'Class;    --  3
  procedure Display (G : in Gadget);                 --  4
  type Doohickey is new Gadget with private;         --  5 
  procedure Display (G : in Doohickey);              --  6
  function  Get return Gadget'Class;                 --  7
private                                              --  8
  type Gadget is tagged record                       --  9
     Is_On : Boolean := False;                       -- 10
  end record;                                        -- 11
  type Doohickey is new Gadget with record           -- 12
     Spinning : Boolean := False;                    -- 13
  end record;                                        -- 14
end Thingamabobs;                                    -- 15

with the following little test program,

with Thingamabobs;                                   --  1
with Ada.Text_IO;                                    --  2
with Ada.Integer_Text_IO;                            --  3
use  Ada;                                            --  4
procedure Test_Thingamabobs is                       --  5
  Gadget_Stuff : array (1..2) of                     --  6     
              Thingamabobs.Gadget_Pointer;           --  7
     := (1 => new Thingamabobs.Gadget,               --  8
         2 => new Thingamabobs.Doohickey);           --  9
  Index : Positive;                                  -- 10 
begin                                                -- 11
   Text_IO.Put("Enter Index Value (1/2) " );         -- 12
   Integer_Text_IO.Get(Index);                       -- 13
   Thingamabobs.Display(Gadget_Stuff(Index).all);    -- 14
end Test_Thingamabobs;                               -- 15

Another way to approach this is to declare the classwide type within
a declare block and constrain it with a function call,

with Thingamabobs;                                   --  1
procedure Get_Thingamabobs is                        --  2
begin                                                --  3
   declare                                           --  4 
      Gadget_Stuff : Thingamabobs.Gadget'Class       --  5
                   := Thingamabobs.Get;              --  6
   begin                                             --  7
      Thingamabobs.Display(Gadget_Stuff);            --  8
   end;                                              --  9
end Get_Thingamabobs;                                -- 10

where the declare block could be placed inside a loop.  Alternatively,
the declare block could (many say _should_) be promoted to a subprogram
call.  Finally, we have a really shortened example that illustrates 
some of the power associated with a classwide function and a dispatching
call,

with Thingamabobs;                                   --  1
procedure Abbreviated_Get_Thingamabobs is            --  2
begin                                                --  3
   Thingamabobs.Display(Thingamabobs.Get);           --  4
end Abbreviated_Get_Thingamabobs ;                   --  5

Typically, the Get will acquire the data from a file, a data
structure, or some other container in which the tag has been
preserved.  For example, you can create a Stream_IO file in
which the tag is persistent. 

Hope this helps,

Richard Riehle
http://www.adaworks.com




  reply	other threads:[~1999-10-15  0:00 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-10-15  0:00 Inheritance and Polymorphism in Ada !! Chango Cho
1999-10-15  0:00 ` Ted Dennison
1999-10-15  0:00 ` Lutz Donnerhacke
1999-10-15  0:00 ` Andreas Winckler
1999-10-15  0:00   ` Richard D Riehle [this message]
1999-10-15  0:00     ` Matthew Heaney
1999-10-15  0:00       ` Richard D Riehle
1999-10-18  0:00       ` Robert Dewar
1999-10-23  0:00         ` Richard D Riehle
1999-10-24  0:00           ` Robert Dewar
1999-10-24  0:00             ` Brian Rogoff
1999-10-26  0:00               ` Robert Dewar
1999-10-25  0:00             ` Robert A Duff
1999-10-26  0:00               ` Robert Dewar
1999-10-26  0:00                 ` Robert A Duff
1999-10-18  0:00       ` Robert A Duff
1999-10-19  0:00         ` Robert Dewar
1999-10-20  0:00           ` Robert A Duff
1999-10-21  0:00             ` Robert Dewar
1999-10-21  0:00             ` Paul Duquennoy
1999-10-21  0:00             ` Simon Wright
1999-10-21  0:00               ` Robert A Duff
1999-10-22  0:00         ` Matthew Heaney
1999-10-15  0:00   ` tmoran
1999-10-15  0:00     ` tmoran
1999-10-15  0:00   ` Stephane Barbey
1999-10-15  0:00   ` Matthew Heaney
1999-10-18  0:00     ` Robert A Duff
1999-10-18  0:00       ` Brian Rogoff
1999-10-15  0:00   ` Lutz Donnerhacke
1999-10-18  0:00   ` Robert A Duff
replies disabled

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