comp.lang.ada
 help / color / mirror / Atom feed
From: markus034@gmail.com
Subject: Re: Translating C++ class types into Ada tagged types?
Date: Mon, 16 Jul 2007 10:35:30 -0700
Date: 2007-07-16T10:35:30-07:00	[thread overview]
Message-ID: <1184607330.524261.22720@o11g2000prd.googlegroups.com> (raw)
In-Reply-To: <469b1dca$0$31622$9b4e6d93@newsspool3.arcor-online.net>

On Jul 16, 12:30 am, Georg Bauhaus
<bauhaus.rm.t...@maps.futureapps.de> wrote:
> markus...@gmail.com wrote:
> > On Jul 10, 1:07 am, Georg Bauhaus <bauhaus.rm.t...@maps.futureapps.de>
> > wrote:
> >> jimmaureenrog...@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
>
> > In two programs written bellow i would like to ask you the following
> > questions:
> > -In "program 1" i used the constructor function. However, I don't
> > understand how
> > to make methods, like Object.method, with the constructor function.
>
> Uhm, just like in Java, constructors are not methods. (Or do I
> misread your question?) This is because before construction has
> finished there no object on which to call methods.
> The purpose of construction is to create an object and initialize its
> components. Once you have an object, and only then, your program can
> perform "methods calls".
> In Ada, a type's methods are named the "primitive operations" of the
> type, those defined together with the type in the same package.
> (They are preferably not called methods, for technical reasons.) In your
> example, the primitive operations of Person are NameOf and Gender.
>
> You have a choice of writing calls of these operations as either
>
>     Obj.Method(Arg1, Arg2, ...);  -- since Ada 2005
> or
>     Method(Obj, Arg1, Arg2, ...);  -- Ada 95
>
> The meaning is the same: There is an object Obj, and you invoke a
> primitive operation with both an object and further arguments.
> The operation might then read or update the obj Obj, print it,
> change a component using the value in Arg1, etc.
>
> See alsohttp://en.wikibooks.org/wiki/Ada_Programming/Object_Orientationhttp://www.cmis.brighton.ac.uk/~je/adacraft/ch14.htm
>
> > -In "program 2" i didn't use any constructor function. My question is
> > do i need to make
> > any constructor function in program 2?
>
> If you don't have explicit construction, the components of
> objects will be default initialized. If this is enough for
> your purpose, you don't need explicit construction.

I didn't express myself clearly enough. However, you gave me the
perfect answer. The answer
i was expected to receive.
Here is what i was expected for program 1:
a) " The purpose of construction is to create an object and initialize
its
components. Once you have an object, and only then, your program can
perform "methods calls".".
b)" In Ada, a type's methods are named the "primitive operations" of
the
type, those defined together with the type in the same package."
c)"You have a choice of writing calls of these operations as either

    Obj.Method(Arg1, Arg2, ...);  -- since Ada 2005
or
    Method(Obj, Arg1, Arg2, ...);  -- Ada 95

The meaning is the same: There is an object Obj, and you invoke a
primitive operation with both an object and further arguments.
The operation might then read or update the obj Obj, print it,
change a component using the value in Arg1, etc.".

And here is what i was expected for program 2:
"If you don't have explicit construction, the components of
objects will be default initialized. If this is enough for
your purpose, you don't need explicit construction."


It seems to me that the ada wikibook doesn't contain any info about
constructor functions.
Therefore, it would be nice if you or someone else add the information
about constructors into
the wikibook for ada.

George, i really appriciate your help.
I added "procedure Dance(The_Person:Person);" to the the program 1,
then called it as follows
"George.Dance;", and now it works ferfectly.
I always used to think that object-oriented programming is weird and
difficult, but now,
I'm not afraid of object-oriented programming in ada.

George, thanks a lot for your help.




  reply	other threads:[~2007-07-16 17:35 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
2007-07-16  5:47     ` markus034
2007-07-16  7:30       ` Georg Bauhaus
2007-07-16 17:35         ` markus034 [this message]
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