From: markus034@gmail.com
Subject: Re: Translating C++ class types into Ada tagged types?
Date: Sun, 15 Jul 2007 22:47:13 -0700
Date: 2007-07-15T22:47:13-07:00 [thread overview]
Message-ID: <1184564833.365076.305390@g12g2000prg.googlegroups.com> (raw)
In-Reply-To: <46933d57$0$21008$9b4e6d93@newsspool1.arcor-online.net>
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.
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;
next prev parent reply other threads:[~2007-07-16 5:47 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 [this message]
2007-07-16 7:30 ` Georg Bauhaus
2007-07-16 17:35 ` markus034
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