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!g12g2000prg.googlegroups.com!not-for-mail From: markus034@gmail.com Newsgroups: comp.lang.ada Subject: Re: Translating C++ class types into Ada tagged types? Date: Sun, 15 Jul 2007 22:47:13 -0700 Organization: http://groups.google.com Message-ID: <1184564833.365076.305390@g12g2000prg.googlegroups.com> References: <1184031587.650366.207550@m37g2000prh.googlegroups.com> <1184033524.336233.241450@d30g2000prg.googlegroups.com> <46933d57$0$21008$9b4e6d93@newsspool1.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 1184564834 3299 127.0.0.1 (16 Jul 2007 05:47:14 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Mon, 16 Jul 2007 05:47:14 +0000 (UTC) In-Reply-To: <46933d57$0$21008$9b4e6d93@newsspool1.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: g12g2000prg.googlegroups.com; posting-host=75.165.1.182; posting-account=ps2QrAMAAAA6_jCuRt2JEIpn5Otqf_w0 Xref: g2news1.google.com comp.lang.ada:16481 Date: 2007-07-15T22:47:13-07:00 List-Id: 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. Does anyone know how to make methods with the constructor function in program 1? -In "program 2" i didn't use any constructor function. My question is do i need to make any constructor function in program 2? Program 1 (with consructors but without methods): --spec 1. PACKAGE Persons IS TYPE Genders IS (Female, Male); SUBTYPE NameRange IS Positive RANGE 1..20; SUBTYPE NameType IS String(NameRange); TYPE Person IS TAGGED PRIVATE; --Selectors FUNCTION NameOf(Whom:Person) RETURN NameType; FUNCTION GenderOf(Whom:Person) RETURN Genders; PROCEDURE Put(Item:IN Person); PACKAGE Constructors IS --constructor function FUNCTION MakePerson(Name:String; Gender:Genders) RETURN Person; END Constructors; PRIVATE TYPE Person IS TAGGED RECORD NameLength:NameRange:=1; NameField:NameType:=(OTHERS =>' '); Gender:Genders:=Female; END RECORD; END Persons; --spec 2. WITH Persons; USE Persons; PACKAGE Employees IS TYPE Employee IS NEW Person WITH PRIVATE; TYPE IDType IS NEW Positive RANGE 1111..9999; --Selectors FUNCTION IDOf(Whom:Employee) RETURN IDType; PROCEDURE Put(Item:IN Employee); PACKAGE Constructors IS --constructor function FUNCTION MakeEmployee(Name:String; Gender:Genders; ID:IDType) RETURN Employee; END Constructors; PRIVATE TYPE Employee IS NEW Person WITH RECORD ID:IDType:=1111; END RECORD; END Employees; --body 1. WITH Ada.Text_IO; USE Ada.Text_IO; PACKAGE BODY Persons IS PACKAGE Gender_IO IS NEW Enumeration_IO(Enum=>Genders); -- TYPE Genders IS (Female, Male); -- SUBTYPE NameRange IS Positive RANGE 1..20; -- SUBTYPE NameType IS String(NameRange); -- TYPE Person IS TAGGED PRIVATE; --Selectors FUNCTION NameOf(Whom:Person) RETURN NameType IS BEGIN RETURN Whom.NameField; END NameOf; FUNCTION GenderOf(Whom:Person) RETURN Genders IS BEGIN RETURN Whom.Gender; END GenderOf; PROCEDURE Put(Item:IN Person) IS BEGIN Put(Item => "Name: "); Put(Item => Item.NameField(1..Item.NameLength)); New_Line; Put(Item => "Gender: "); Gender_Io.Put(Item=>Item.Gender, Set=>Lower_Case); New_Line; END Put; PACKAGE BODY Constructors IS --constructor function FUNCTION MakePerson(Name:String; Gender:Genders) RETURN Person IS Temp:NameType; BEGIN Temp(1..Name'Length):=Name; RETURN (NameLength=>Name'Length, NameField=>Temp, Gender=>Gender); END MakePerson; END Constructors; -- PRIVATE -- TYPE Person IS TAGGED RECORD -- NameLength:NameRange:=1; -- NameField:NameType:=(OTHERS =>' '); -- Gender:Genders:=Female; -- END RECORD; -- END Persons; --body 2. WITH Ada.Text_IO; USE Ada.Text_IO; WITH Ada.Integer_Text_IO; USE Ada.Integer_Text_IO; PACKAGE BODY Employees IS PACKAGE BODY Constructors IS --constructor function FUNCTION MakeEmployee(Name:String; Gender:Genders; ID:IDType) RETURN Employee is BEGIN RETURN (Persons.Constructors.MakePerson( Name=>Name, Gender=>Gender) WITH ID=>ID); END MakeEmployee; END Constructors; --Selectors FUNCTION IDOf(Whom:Employee) RETURN IDType IS BEGIN RETURN Whom.ID; END IDOf; PROCEDURE Put(Item:IN Employee) IS BEGIN Persons.Put(Item => Persons.Person(Item)); Put(Item => "ID Number: "); Put(Item => Positive(Item.ID), Width=>1); END Put; END Employees; --main program WITH Ada.Text_IO; USE Ada.Text_IO; WITH Persons; USE Persons; WITH Employees; USE Employees; PROCEDURE Main IS George:Person; Mary:Employee; BEGIN --first construct a person and an employee George:=Persons.Constructors.MakePerson( Name=>"George", Gender=> Male); Mary:=Employees.Constructors.MakeEmployee( Name=>"Mary", Gender=>Female, ID=>1234); --Now display them Put(Item => George); Put_Line(Item => "--------------------"); Put(Item => Mary); END Main; Program 2 (without constructors but with methods): --spec 1 package People is type Person is tagged private; function Get_Age(The_Person : Person) return Positive; procedure Set_Age(The_Person : in out Person; Age : in Positive); procedure Sing(The_Person : Person); private type Person is tagged record Age : Positive; end record; end People; --body 1 WITH Ada.Text_IO; package body People is ------------- -- Get_Age -- ------------- function Get_Age (The_Person : Person) return Positive is begin return The_Person.Age; end Get_Age; ------------- -- Set_Age -- ------------- procedure Set_Age (The_Person : in out Person; Age : in Positive) is begin The_Person.Age := Age; end Set_Age; ---------- -- Sing -- ---------- procedure Sing(The_Person : Person) is begin Ada.Text_IO.Put_Line(" Can sing."); end Sing; end People; --spec2 WITH People; USE People; PACKAGE Programmers IS TYPE Programmer IS TAGGED PRIVATE; function Get_Age(The_Programmer : Programmer) return Positive; procedure Set_Age(The_Programmer : in out Programmer; Age : in Positive); procedure Program(The_Programmer : Programmer); private type Programmer is new Person with record Age : Positive; end record; end Programmers; --body 2 WITH Ada.Text_IO; package body Programmers is ------------- -- Get_Age -- ------------- function Get_Age (The_Programmer : Programmer) return Positive is begin return The_Programmer.Age; end Get_Age; ------------- -- Set_Age -- ------------- procedure Set_Age (The_Programmer : in out Programmer; Age : in Positive) is begin The_Programmer.Age := Age; end Set_Age; ------------- -- Program -- ------------- procedure Program(The_Programmer : Programmer) is begin Ada.Text_IO.Put_Line(" Can program."); end Program; end Programmers; --main program WITH People; WITH Programmers; USE TYPE People.Person; USE TYPE Programmers.Programmer; WITH Ada.Text_Io; procedure Person_Main is George : People.Person; Andy:Programmers.Programmer; begin George.Set_Age(20); Andy.Set_Age(29); Ada.Text_Io.Put("George: "); Ada.Text_Io.Put_Line(Positive'Image(George.Get_Age) & " years old."); Ada.Text_IO.New_Line; George.Sing; Ada.Text_IO.Put_Line(Item => "--------------------"); Ada.Text_Io.Put("Andy: "); Ada.Text_Io.Put_Line(Positive'Image(Andy.Get_Age) & " years old."); Ada.Text_IO.New_Line; Andy.Program; end Person_Main;