From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Thread: 103376,fcc68f44249afbd4 X-Google-Attributes: gid103376,public,usenet X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!news1.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail Date: Mon, 16 Jul 2007 09:30:50 +0200 From: Georg Bauhaus Organization: # User-Agent: Thunderbird 1.5.0.12 (Macintosh/20070509) MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: Translating C++ class types into Ada tagged types? References: <1184031587.650366.207550@m37g2000prh.googlegroups.com> <1184033524.336233.241450@d30g2000prg.googlegroups.com> <46933d57$0$21008$9b4e6d93@newsspool1.arcor-online.net> <1184564833.365076.305390@g12g2000prg.googlegroups.com> In-Reply-To: <1184564833.365076.305390@g12g2000prg.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <469b1dca$0$31622$9b4e6d93@newsspool3.arcor-online.net> NNTP-Posting-Date: 16 Jul 2007 09:27:06 CEST NNTP-Posting-Host: 8de3b9ad.newsspool3.arcor-online.net X-Trace: DXC=l:\ocJS2H>RFm0Y?OE@2^XMcF=Q^Z^V3X4Fo<]lROoRQ^YC2XCjHcbY2=?>hIGM markus034@gmail.com wrote: > On Jul 10, 1:07 am, Georg Bauhaus > 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 also http://en.wikibooks.org/wiki/Ada_Programming/Object_Orientation http://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.