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=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no 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!postnews.google.com!o11g2000prd.googlegroups.com!not-for-mail From: markus034@gmail.com Newsgroups: comp.lang.ada Subject: Re: Translating C++ class types into Ada tagged types? Date: Mon, 16 Jul 2007 10:35:30 -0700 Organization: http://groups.google.com Message-ID: <1184607330.524261.22720@o11g2000prd.googlegroups.com> 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> <469b1dca$0$31622$9b4e6d93@newsspool3.arcor-online.net> NNTP-Posting-Host: 75.165.1.182 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1184607331 26479 127.0.0.1 (16 Jul 2007 17:35:31 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 16 Jul 2007 17:35:31 +0000 (UTC) In-Reply-To: <469b1dca$0$31622$9b4e6d93@newsspool3.arcor-online.net> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: o11g2000prd.googlegroups.com; posting-host=75.165.1.182; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news1.google.com comp.lang.ada:16492 Date: 2007-07-16T10:35:30-07:00 List-Id: On Jul 16, 12:30 am, Georg Bauhaus wrote: > markus...@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 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.