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.8 required=5.0 tests=BAYES_00,INVALID_DATE autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,961b968d014d8843 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 1995-01-20 09:41:50 PST Path: nntp.gmd.de!newsserver.jvnc.net!howland.reston.ans.net!darwin.sura.net!RBSE.Mountain.Net!wvnvms!marshall.wvnet.edu!marshall.edu!hathawa2 Newsgroups: comp.lang.ada Subject: Re: Ada Objects Help! Message-ID: <1995Jan20.134150@hobbit> From: hathawa2@marshall.edu (Mark S. Hathaway) Date: 20 Jan 95 13:41:50 EDT References: <3f9g1u$j4m@nps.navy.mil> <1995Jan16.132400@lglsun.epfl.ch> Distribution: world Organization: Marshall University Nntp-Posting-Host: frodo.marshall.edu Date: 1995-01-20T13:41:50-04:00 List-Id: > In article <1995Jan16.132400@lglsun.epfl.ch>, > nebbe@lglsun.epfl.ch (Robb Nebbe) writes: >> In article <3f9g1u$j4m@nps.navy.mil>, swdecato@nps.navy.mil writes: >> I wan't to duplicate the following C++ code in Ada. >> >> ------------------------------------------------------------------- >> #include >> class myclass { >> public: >> myclass(int); >> void display(); >> private: >> int dataelement; >> }; >> >> void myclass::display() { >> cout << dataelement; >> } >> >> int main() >> { >> myclass *myptr = new myclass(10); >> myptr->display(); >> return 1; >> } >> ------------------------------------------------------------------------ > As usual the major problem is how much of the information in your > example is noise and how much is really something you would like > to know. With out more information on the context I'm going to make > a _guess_ as to what you want to know (correct me if I'm wrong). > > package P is -- Just the specification > type T is tagged private; > function Create( X : Integer ) return T; > procedure Display( X : T ); > private > type T is tagged > record > Data_Element : Integer; > end record; > end My_Package; > > with P; use P; > procedure Example is > Object : T'Class := Create( 10 ); > begin > Display( Object ); > end Example; > > Semantically this is probably as close as you can get to your C++ > example. In Ada the pointer isn't needed to dispatch since Ada > differentiates between specific types and class-wide types. Moreover, > in Ada there aren't any problems with your objects getting truncated > so this solution works just fine. There are also other minor variations > possible, depending on what you really want to accomplish. >> How can Ada be object oriented if you always have to pass the data to the >> function? > In C++ you must also pass the data to a function; the only difference > is in the syntax. Object.Operation; is no different semantically than > Operation( Object ); These examples looks odd to me. I'm not very knowledgeable of Ada or C++, but I know pornography when I see it (oops, that's another discussion). :-) It's not obvious if 'myclass(int)' is creating a list of objects of type myclass or if it's assigning 10 --> dataelement. It's not very eloquent. Then the Ada example changes 'myclass(int)' to 'Create(X : Integer) return T' with the added complexity of defining T. It looks wrong or overly complex. And it's comparable in length to the C++ version only because the class implementation isn't given. Does the Ada example even create an object? Here's my idea of how it should look. spirit myclass is proc assign ( x is in integer ); -- assign value referenced by x to myclass proc display (); end myclass; body myclass is var dataelement is integer; proc assign ( x is in integer ) is dataelement := x; end assign; proc display () is out.write(dataelement); end display; end myclass; program example1 is -- This example demonstrates the pointer method the C++ example used. var op is pointer to myclass; begin new(op); -- create instance of an object of type to which op can point op->assign(10); op->display(); end example1. program example2 is -- This example demonstrates the direct usage of an object. obj o is myclass; -- create one instance of the object begin o.assign(10); o.display(); end example2. Isn't this closer to what the C++ example is "saying"? Mark S. Hathaway