comp.lang.ada
 help / color / mirror / Atom feed
From: Georg Bauhaus <bauhaus.rm.tsoh@maps.futureapps.de>
Subject: Re: Translating C++ class types into Ada tagged types?
Date: Tue, 10 Jul 2007 10:07:12 +0200
Date: 2007-07-10T10:03:36+02:00	[thread overview]
Message-ID: <46933d57$0$21008$9b4e6d93@newsspool1.arcor-online.net> (raw)
In-Reply-To: <1184033524.336233.241450@d30g2000prg.googlegroups.com>

jimmaureenrogers@worldnet.att.net wrote:

> 
> Ada is object oriented, but does not provide constructors or
> destructors. The following code is similar to your example
> without constructors or destructors.

Just to add that Ada does have construction and destruction
(I am sure Jim Rogers knows them well), the language provides
a number of ways to make sure that initialization and
finalization takes place (and when it takes place!).
So "Cat Frisky(5)" can be expressed in Ada as well, just not
using constructors as in C++. 

One such facility well suited to polymorphic objects uses a
factory function such as

   function Make 
     (species: String; age: Natural) return Animal'class;
    -- an animal as requested in `species`, `age` years old

where Animal could be a superclass of Cat in C++ terms.
The 'class in Animal'class means that Make will return an object
from the Animal hierarchy. If you wanted just a Cat or more
specific heirs of Cat, say a cat that has fur of some color,
use

   function Make 
     (fur_color: Color; age: Natural) return Cat'class;
    -- some cat whose fur has the given `fur_color`,
    -- `age` years old

And, finally, a simple construction function for Cat specifically
and not heirs, but simply Cats could be

   function Make 
     (age: Natural) return Cat;
    -- a cat, `age` years old

(Unlike C++, Ada can use the type of the return value (Cat)
in order to decide what specific type of object to expect on
the LHS of ":=", for example.)

This isn't the only way to initialize objects, either
at run-time, or at compile time. In particular, you can
have default initialization etc..


 -- Georg



  parent reply	other threads:[~2007-07-10  8:07 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-10  1:39 Translating C++ class types into Ada tagged types? markus034
2007-07-10  2:12 ` jimmaureenrogers
2007-07-10  3:26   ` markus034
2007-07-10  6:27     ` tmoran
2007-07-18 10:27       ` Gerd
2007-07-18 11:58         ` Georg Bauhaus
2007-07-10  8:07   ` Georg Bauhaus [this message]
2007-07-16  5:47     ` markus034
2007-07-16  7:30       ` Georg Bauhaus
2007-07-16 17:35         ` markus034
2007-07-10  9:19 ` Patrick
2007-07-11 20:09   ` markus034
2007-07-11 23:15     ` Jeffrey Creem
2007-07-12  5:46   ` vgodunko
replies disabled

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