comp.lang.ada
 help / color / mirror / Atom feed
* Translating C++ class types into Ada tagged types?
@ 2007-07-10  1:39 markus034
  2007-07-10  2:12 ` jimmaureenrogers
  2007-07-10  9:19 ` Patrick
  0 siblings, 2 replies; 14+ messages in thread
From: markus034 @ 2007-07-10  1:39 UTC (permalink / raw)


Does ada fully support the object-orientedness? If so, how do i write
(or translate) the following C++ object-oriented code into Ada object-
oriented code?
Without constractors:
1:   // Demonstrates declaration of a class and
2:   // definition of class methods,
3:
4:   #include <iostream.h>      // for cout
5:
6:   class Cat                   // begin declaration of the class
7:   {
8:     public:                   // begin public section
9:       int GetAge();           // accessor function
10:      void SetAge (int age);  // accessor function
11:      void Meow();            // general function
12:    private:                  // begin private section
13:      int itsAge;             // member variable
14:  };
15:
16:  // GetAge, Public accessor function
17:  // returns value of itsAge member
18:  int Cat::GetAge()
19:  {
20:     return itsAge;
21:  }
22:
23:  // definition of SetAge, public
24:  // accessor function
25:  // returns sets itsAge member
26:  void Cat::SetAge(int age)
27:  {
28:     // set member variable its age to
29:     // value passed in by parameter age
30:     itsAge = age;
31:  }
32:
33:  // definition of Meow method
34:  // returns: void
35:  // parameters: None
36:  // action: Prints "meow" to screen
37:  void Cat::Meow()
38:  {
39:     cout << "Meow.\n";
40:  }
41:
42:  // create a cat, set its age, have it
43:  // meow, tell us its age, then meow again.
44:  int main()
45:  {
46:     Cat Frisky;
47:     Frisky.SetAge(5);
48:     Frisky.Meow();
49:     cout << "Frisky is a cat who is " ;
50:     cout << Frisky.GetAge() << " years old.\n";
51:     Frisky.Meow();
52;      return 0;
53: }
Also the same code with constractors:
1:   // Demonstrates declaration of a constructors and
2:   // destructor for the Cat class
3:
4:   #include <iostream.h>      // for cout
5:
6:   class Cat                   // begin declaration of the class
7:   {
8:    public:                    // begin public section
9:      Cat(int initialAge);     // constructor
10:     ~Cat();                  // destructor
11:     int GetAge();            // accessor function
12:     void SetAge(int age);    // accessor function
13:     void Meow();
14:   private:                   // begin private section
15:     int itsAge;              // member variable
16:  };
17:
18:  // constructor of Cat,
19:  Cat::Cat(int initialAge)
20:  {
21:     itsAge = initialAge;
22:  }
23:
24:  Cat::~Cat()                 // destructor, takes no action
25:  {
26:  }
27:
28:  // GetAge, Public accessor function
29:  // returns value of itsAge member
30:  int Cat::GetAge()
31:  {
32:     return itsAge;
33:  }
34:
35:  // Definition of SetAge, public
36:  // accessor function
37:
38:  void Cat::SetAge(int age)
39:  {
40:     // set member variable its age to
41:     // value passed in by parameter age
42:     itsAge = age;
43:  }
44:
45:  // definition of Meow method
46:  // returns: void
47:  // parameters: None
48:  // action: Prints "meow" to screen
49:  void Cat::Meow()
50:  {
51:     cout << "Meow.\n";
52:  }
53:
54:  // create a cat, set its age, have it
55   // meow, tell us its age, then meow again.
56:  int main()
57:  {
58:    Cat Frisky(5);
59:    Frisky.Meow();
60:    cout << "Frisky is a cat who is " ;
61:    cout << Frisky.GetAge() << " years old.\n";
62:    Frisky.Meow();
63:    Frisky.SetAge(7);
64:    cout << "Now Frisky is " ;
65:    cout << Frisky.GetAge() << " years old.\n";
66;     return 0;
67: }




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  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  8:07   ` Georg Bauhaus
  2007-07-10  9:19 ` Patrick
  1 sibling, 2 replies; 14+ messages in thread
From: jimmaureenrogers @ 2007-07-10  2:12 UTC (permalink / raw)


On Jul 9, 7:39 pm, markus...@gmail.com wrote:
> Does ada fully support the object-orientedness? If so, how do i write
> (or translate) the following C++ object-oriented code into Ada object-
> oriented code?
> Without constractors:
> 1:   // Demonstrates declaration of a class and
> 2:   // definition of class methods,
> 3:
> 4:   #include <iostream.h>      // for cout
> 5:
> 6:   class Cat                   // begin declaration of the class
> 7:   {
> 8:     public:                   // begin public section
> 9:       int GetAge();           // accessor function
> 10:      void SetAge (int age);  // accessor function
> 11:      void Meow();            // general function
> 12:    private:                  // begin private section
> 13:      int itsAge;             // member variable
> 14:  };
> 15:
> 16:  // GetAge, Public accessor function
> 17:  // returns value of itsAge member
> 18:  int Cat::GetAge()
> 19:  {
> 20:     return itsAge;
> 21:  }
> 22:
> 23:  // definition of SetAge, public
> 24:  // accessor function
> 25:  // returns sets itsAge member
> 26:  void Cat::SetAge(int age)
> 27:  {
> 28:     // set member variable its age to
> 29:     // value passed in by parameter age
> 30:     itsAge = age;
> 31:  }
> 32:
> 33:  // definition of Meow method
> 34:  // returns: void
> 35:  // parameters: None
> 36:  // action: Prints "meow" to screen
> 37:  void Cat::Meow()
> 38:  {
> 39:     cout << "Meow.\n";
> 40:  }
> 41:
> 42:  // create a cat, set its age, have it
> 43:  // meow, tell us its age, then meow again.
> 44:  int main()
> 45:  {
> 46:     Cat Frisky;
> 47:     Frisky.SetAge(5);
> 48:     Frisky.Meow();
> 49:     cout << "Frisky is a cat who is " ;
> 50:     cout << Frisky.GetAge() << " years old.\n";
> 51:     Frisky.Meow();
> 52;      return 0;
> 53: }
> Also the same code with constractors:
> 1:   // Demonstrates declaration of a constructors and
> 2:   // destructor for the Cat class
> 3:
> 4:   #include <iostream.h>      // for cout
> 5:
> 6:   class Cat                   // begin declaration of the class
> 7:   {
> 8:    public:                    // begin public section
> 9:      Cat(int initialAge);     // constructor
> 10:     ~Cat();                  // destructor
> 11:     int GetAge();            // accessor function
> 12:     void SetAge(int age);    // accessor function
> 13:     void Meow();
> 14:   private:                   // begin private section
> 15:     int itsAge;              // member variable
> 16:  };
> 17:
> 18:  // constructor of Cat,
> 19:  Cat::Cat(int initialAge)
> 20:  {
> 21:     itsAge = initialAge;
> 22:  }
> 23:
> 24:  Cat::~Cat()                 // destructor, takes no action
> 25:  {
> 26:  }
> 27:
> 28:  // GetAge, Public accessor function
> 29:  // returns value of itsAge member
> 30:  int Cat::GetAge()
> 31:  {
> 32:     return itsAge;
> 33:  }
> 34:
> 35:  // Definition of SetAge, public
> 36:  // accessor function
> 37:
> 38:  void Cat::SetAge(int age)
> 39:  {
> 40:     // set member variable its age to
> 41:     // value passed in by parameter age
> 42:     itsAge = age;
> 43:  }
> 44:
> 45:  // definition of Meow method
> 46:  // returns: void
> 47:  // parameters: None
> 48:  // action: Prints "meow" to screen
> 49:  void Cat::Meow()
> 50:  {
> 51:     cout << "Meow.\n";
> 52:  }
> 53:
> 54:  // create a cat, set its age, have it
> 55   // meow, tell us its age, then meow again.
> 56:  int main()
> 57:  {
> 58:    Cat Frisky(5);
> 59:    Frisky.Meow();
> 60:    cout << "Frisky is a cat who is " ;
> 61:    cout << Frisky.GetAge() << " years old.\n";
> 62:    Frisky.Meow();
> 63:    Frisky.SetAge(7);
> 64:    cout << "Now Frisky is " ;
> 65:    cout << Frisky.GetAge() << " years old.\n";
> 66;     return 0;
> 67: }

Ada is object oriented, but does not provide constructors or
destructors. The following code is similar to your example
without constructors or destructors.

Ada uses packages for encapsulation. The following package defines
a Cat tagged type and three methods for that type. The package
specification
provides all interface definitions for Cat.

package Animals is
   type Cat is tagged private;
   function Get_Age(Fluffy : Cat) return Positive;
   procedure Set_Age(The_Cat : in out Cat; Age : in Positive);
   procedure Meow(The_Cat : Cat);
private
   type Cat is tagged record
      Age : Positive;
   end record;
end Animals;

with Ada.Text_Io;

The following package body contains the implementation of the
function and two procedures.

package body Animals is

   -------------
   -- Get_Age --
   -------------

   function Get_Age (Fluffy : Cat) return Positive is
   begin
      return Fluffy.Age;
   end Get_Age;

   -------------
   -- Set_Age --
   -------------

   procedure Set_Age (The_Cat : in out Cat; Age : in Positive) is
   begin
      The_Cat.Age := Age;
   end Set_Age;

   ----------
   -- Meow --
   ----------

   procedure Meow(The_Cat : Cat) is
   begin
      Ada.Text_IO.Put_Line("Meow.");
   end Meow;

end Animals;

Finally, here is the entry point procedure equivalent to the C++
main function.

with Animals;
use type Animals.Cat;
with Ada.Text_Io;

procedure Cat_Main is
   Frisky : Animals.Cat;
begin
   Frisky.Set_Age(5);
   Frisky.Meow;
   Ada.Text_Io.Put("Frisky is a cat who is");
   Ada.Text_Io.Put_Line(Positive'Image(Frisky.Get_Age) & " years
old.");
end Cat_Main;


Jim Rogers





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  2:12 ` jimmaureenrogers
@ 2007-07-10  3:26   ` markus034
  2007-07-10  6:27     ` tmoran
  2007-07-10  8:07   ` Georg Bauhaus
  1 sibling, 1 reply; 14+ messages in thread
From: markus034 @ 2007-07-10  3:26 UTC (permalink / raw)


On Jul 9, 7:12 pm, "jimmaureenrog...@worldnet.att.net"
<jimmaureenrog...@worldnet.att.net> wrote:
> On Jul 9, 7:39 pm, markus...@gmail.com wrote:
>
>
>
> > Does ada fully support the object-orientedness? If so, how do i write
> > (or translate) the following C++ object-oriented code into Ada object-
> > oriented code?
> > Without constractors:
> > 1:   // Demonstrates declaration of a class and
> > 2:   // definition of class methods,
> > 3:
> > 4:   #include <iostream.h>      // for cout
> > 5:
> > 6:   class Cat                   // begin declaration of the class
> > 7:   {
> > 8:     public:                   // begin public section
> > 9:       int GetAge();           // accessor function
> > 10:      void SetAge (int age);  // accessor function
> > 11:      void Meow();            // general function
> > 12:    private:                  // begin private section
> > 13:      int itsAge;             // member variable
> > 14:  };
> > 15:
> > 16:  // GetAge, Public accessor function
> > 17:  // returns value of itsAge member
> > 18:  int Cat::GetAge()
> > 19:  {
> > 20:     return itsAge;
> > 21:  }
> > 22:
> > 23:  // definition of SetAge, public
> > 24:  // accessor function
> > 25:  // returns sets itsAge member
> > 26:  void Cat::SetAge(int age)
> > 27:  {
> > 28:     // set member variable its age to
> > 29:     // value passed in by parameter age
> > 30:     itsAge = age;
> > 31:  }
> > 32:
> > 33:  // definition of Meow method
> > 34:  // returns: void
> > 35:  // parameters: None
> > 36:  // action: Prints "meow" to screen
> > 37:  void Cat::Meow()
> > 38:  {
> > 39:     cout << "Meow.\n";
> > 40:  }
> > 41:
> > 42:  // create a cat, set its age, have it
> > 43:  // meow, tell us its age, then meow again.
> > 44:  int main()
> > 45:  {
> > 46:     Cat Frisky;
> > 47:     Frisky.SetAge(5);
> > 48:     Frisky.Meow();
> > 49:     cout << "Frisky is a cat who is " ;
> > 50:     cout << Frisky.GetAge() << " years old.\n";
> > 51:     Frisky.Meow();
> > 52;      return 0;
> > 53: }
> > Also the same code with constractors:
> > 1:   // Demonstrates declaration of a constructors and
> > 2:   // destructor for the Cat class
> > 3:
> > 4:   #include <iostream.h>      // for cout
> > 5:
> > 6:   class Cat                   // begin declaration of the class
> > 7:   {
> > 8:    public:                    // begin public section
> > 9:      Cat(int initialAge);     // constructor
> > 10:     ~Cat();                  // destructor
> > 11:     int GetAge();            // accessor function
> > 12:     void SetAge(int age);    // accessor function
> > 13:     void Meow();
> > 14:   private:                   // begin private section
> > 15:     int itsAge;              // member variable
> > 16:  };
> > 17:
> > 18:  // constructor of Cat,
> > 19:  Cat::Cat(int initialAge)
> > 20:  {
> > 21:     itsAge = initialAge;
> > 22:  }
> > 23:
> > 24:  Cat::~Cat()                 // destructor, takes no action
> > 25:  {
> > 26:  }
> > 27:
> > 28:  // GetAge, Public accessor function
> > 29:  // returns value of itsAge member
> > 30:  int Cat::GetAge()
> > 31:  {
> > 32:     return itsAge;
> > 33:  }
> > 34:
> > 35:  // Definition of SetAge, public
> > 36:  // accessor function
> > 37:
> > 38:  void Cat::SetAge(int age)
> > 39:  {
> > 40:     // set member variable its age to
> > 41:     // value passed in by parameter age
> > 42:     itsAge = age;
> > 43:  }
> > 44:
> > 45:  // definition of Meow method
> > 46:  // returns: void
> > 47:  // parameters: None
> > 48:  // action: Prints "meow" to screen
> > 49:  void Cat::Meow()
> > 50:  {
> > 51:     cout << "Meow.\n";
> > 52:  }
> > 53:
> > 54:  // create a cat, set its age, have it
> > 55   // meow, tell us its age, then meow again.
> > 56:  int main()
> > 57:  {
> > 58:    Cat Frisky(5);
> > 59:    Frisky.Meow();
> > 60:    cout << "Frisky is a cat who is " ;
> > 61:    cout << Frisky.GetAge() << " years old.\n";
> > 62:    Frisky.Meow();
> > 63:    Frisky.SetAge(7);
> > 64:    cout << "Now Frisky is " ;
> > 65:    cout << Frisky.GetAge() << " years old.\n";
> > 66;     return 0;
> > 67: }
>
> Ada is object oriented, but does not provide constructors or
> destructors. The following code is similar to your example
> without constructors or destructors.
>
> Ada uses packages for encapsulation. The following package defines
> a Cat tagged type and three methods for that type. The package
> specification
> provides all interface definitions for Cat.
>
> package Animals is
>    type Cat is tagged private;
>    function Get_Age(Fluffy : Cat) return Positive;
>    procedure Set_Age(The_Cat : in out Cat; Age : in Positive);
>    procedure Meow(The_Cat : Cat);
> private
>    type Cat is tagged record
>       Age : Positive;
>    end record;
> end Animals;
>
> with Ada.Text_Io;
>
> The following package body contains the implementation of the
> function and two procedures.
>
> package body Animals is
>
>    -------------
>    -- Get_Age --
>    -------------
>
>    function Get_Age (Fluffy : Cat) return Positive is
>    begin
>       return Fluffy.Age;
>    end Get_Age;
>
>    -------------
>    -- Set_Age --
>    -------------
>
>    procedure Set_Age (The_Cat : in out Cat; Age : in Positive) is
>    begin
>       The_Cat.Age := Age;
>    end Set_Age;
>
>    ----------
>    -- Meow --
>    ----------
>
>    procedure Meow(The_Cat : Cat) is
>    begin
>       Ada.Text_IO.Put_Line("Meow.");
>    end Meow;
>
> end Animals;
>
> Finally, here is the entry point procedure equivalent to the C++
> main function.
>
> with Animals;
> use type Animals.Cat;
> with Ada.Text_Io;
>
> procedure Cat_Main is
>    Frisky : Animals.Cat;
> begin
>    Frisky.Set_Age(5);
>    Frisky.Meow;
>    Ada.Text_Io.Put("Frisky is a cat who is");
>    Ada.Text_Io.Put_Line(Positive'Image(Frisky.Get_Age) & " years
> old.");
> end Cat_Main;
>
> Jim Rogers

The code you translated works perfectly. I've been thinking on how to
translate that code for about two weeks and i couldn't figure it out.
Now i begin to understand how object-orientation works in ada.
Jim Rogers, I really appreciate your help.
Thanks a lot.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  3:26   ` markus034
@ 2007-07-10  6:27     ` tmoran
  2007-07-18 10:27       ` Gerd
  0 siblings, 1 reply; 14+ messages in thread
From: tmoran @ 2007-07-10  6:27 UTC (permalink / raw)


You can force the user to give an initial age by

 package Animals is
    type Cat(Initial_Age : Positive) is tagged private;
 ...
    type Cat(Initial_Age : Positive) is tagged record
       Age : Positive := Initial_Age;
    end record;
 ...
    Frisky : Animals.Cat(Initial_Age => 0);
If you want the initialization to be significantly more complicated
than that, you can make type Cat controlled, with Initialize and
Finalize procedures.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  2:12 ` jimmaureenrogers
  2007-07-10  3:26   ` markus034
@ 2007-07-10  8:07   ` Georg Bauhaus
  2007-07-16  5:47     ` markus034
  1 sibling, 1 reply; 14+ messages in thread
From: Georg Bauhaus @ 2007-07-10  8:07 UTC (permalink / raw)


jimmaureenrogers@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



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  1:39 Translating C++ class types into Ada tagged types? markus034
  2007-07-10  2:12 ` jimmaureenrogers
@ 2007-07-10  9:19 ` Patrick
  2007-07-11 20:09   ` markus034
  2007-07-12  5:46   ` vgodunko
  1 sibling, 2 replies; 14+ messages in thread
From: Patrick @ 2007-07-10  9:19 UTC (permalink / raw)


markus034@gmail.com a �crit :
> Does ada fully support the object-orientedness? If so, how do i write
> (or translate) the following C++ object-oriented code into Ada object-
> oriented code?

By the way, how about translating C++ multiple inheritance into Ada?
I've read that someone worked on a SWIG module for Ada; do you know
about this?



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  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
  1 sibling, 1 reply; 14+ messages in thread
From: markus034 @ 2007-07-11 20:09 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 582 bytes --]

On Jul 10, 2:19 am, Patrick <peuimpo...@quelquepart.fr> wrote:
> markus...@gmail.com a écrit :
>
> > Does ada fully support the object-orientedness? If so, how do i write
> > (or translate) the following C++ object-oriented code into Ada object-
> > oriented code?
>
> By the way, how about translating C++ multiple inheritance into Ada?
> I've read that someone worked on a SWIG module for Ada; do you know
> about this?


> >I've read that someone worked on a SWIG module for Ada; do you know
> >about this?

No, I never heard about the SWIG module for Ada.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-11 20:09   ` markus034
@ 2007-07-11 23:15     ` Jeffrey Creem
  0 siblings, 0 replies; 14+ messages in thread
From: Jeffrey Creem @ 2007-07-11 23:15 UTC (permalink / raw)


markus034@gmail.com wrote:
> On Jul 10, 2:19 am, Patrick <peuimpo...@quelquepart.fr> wrote:
>> markus...@gmail.com a �crit :
>>
>>> Does ada fully support the object-orientedness? If so, how do i write
>>> (or translate) the following C++ object-oriented code into Ada object-
>>> oriented code?
>> By the way, how about translating C++ multiple inheritance into Ada?
>> I've read that someone worked on a SWIG module for Ada; do you know
>> about this?
> 
> 
>>> I've read that someone worked on a SWIG module for Ada; do you know
>>> about this?
> 
> No, I never heard about the SWIG module for Ada.
> 
For a short overview of swig see
http://www.swig.org/

to take a look at the work in process for Adding Ada support see:

http://gnuada.svn.sourceforge.net/viewvc/gnuada/trunk/projects/swig-1.3.29/





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  9:19 ` Patrick
  2007-07-11 20:09   ` markus034
@ 2007-07-12  5:46   ` vgodunko
  1 sibling, 0 replies; 14+ messages in thread
From: vgodunko @ 2007-07-12  5:46 UTC (permalink / raw)


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain; charset="us-ascii", Size: 755 bytes --]

On 10    , 13:19, Patrick <peuimpo...@quelquepart.fr> wrote:
> markus...@gmail.com a écrit :
>
> > Does ada fully support the object-orientedness? If so, how do i write
> > (or translate) the following C++ object-oriented code into Ada object-
> > oriented code?
>
> By the way, how about translating C++ multiple inheritance into Ada?
> I've read that someone worked on a SWIG module for Ada; do you know
> about this?
In QtAda project (http://sourceforge.net/projects/qtada/) you may find
the way for creating portable bindings to C++ code.

In Ada Modeling Framework project (http://sourceforge.net/projects/
adamof) you may see how class hierarhies from MOF and UML
specifications mapped into Ada2005 interface and tagged types.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  8:07   ` Georg Bauhaus
@ 2007-07-16  5:47     ` markus034
  2007-07-16  7:30       ` Georg Bauhaus
  0 siblings, 1 reply; 14+ messages in thread
From: markus034 @ 2007-07-16  5:47 UTC (permalink / raw)


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;




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-16  5:47     ` markus034
@ 2007-07-16  7:30       ` Georg Bauhaus
  2007-07-16 17:35         ` markus034
  0 siblings, 1 reply; 14+ messages in thread
From: Georg Bauhaus @ 2007-07-16  7:30 UTC (permalink / raw)


markus034@gmail.com wrote:
> 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.

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.



^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-16  7:30       ` Georg Bauhaus
@ 2007-07-16 17:35         ` markus034
  0 siblings, 0 replies; 14+ messages in thread
From: markus034 @ 2007-07-16 17:35 UTC (permalink / raw)


On Jul 16, 12:30 am, Georg Bauhaus
<bauhaus.rm.t...@maps.futureapps.de> wrote:
> markus...@gmail.com wrote:
> > 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.
>
> 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.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-10  6:27     ` tmoran
@ 2007-07-18 10:27       ` Gerd
  2007-07-18 11:58         ` Georg Bauhaus
  0 siblings, 1 reply; 14+ messages in thread
From: Gerd @ 2007-07-18 10:27 UTC (permalink / raw)



tmo...@acm.org schrieb:
> You can force the user to give an initial age by
>
>  package Animals is
>     type Cat(Initial_Age : Positive) is tagged private;
>  ...
>     type Cat(Initial_Age : Positive) is tagged record
>        Age : Positive := Initial_Age;
>     end record;
>  ...
>     Frisky : Animals.Cat(Initial_Age => 0);
> If you want the initialization to be significantly more complicated
> than that, you can make type Cat controlled, with Initialize and
> Finalize procedures.


Is this dot notation a new feature of Ada 2005? As far as I know (my
own trials, long ago) it is not possible with Ada95.
     Frisky : Animals.Cat(Initial_Age => 0);


Lets assume the following code (maybe not syntactically correct),
would it work this way, or how would it be done?

package Vehicle is
  type Car is tagged record
    NrWheels : Positive := 4;
  end record;

 function Wheels (c : Car'Class) return Positive;
end Vehicle;

with Vehicle
package TransportCar is
  type BigCar is new Car with
    record
       Seats : Positive;
    end record;

  function Seats (bc : BigCar) return Positive;

end TransportCar;

...

with Car;
with TransportCar;

MyCar : TransportCar;

...

nrseat := MyCar.Seats;
nswheels := MyCar.Wheels;

Does the compiler know where the functions come from, or must I write
it like in

nrwheels := Car.Wheels (MyCar);
nrseats := TransportCar.Seat(MyCar)




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Translating C++ class types into Ada tagged types?
  2007-07-18 10:27       ` Gerd
@ 2007-07-18 11:58         ` Georg Bauhaus
  0 siblings, 0 replies; 14+ messages in thread
From: Georg Bauhaus @ 2007-07-18 11:58 UTC (permalink / raw)


On Wed, 2007-07-18 at 03:27 -0700, Gerd wrote:

> nrseat := MyCar.Seats;

http://www.adaic.org/standards/05rat/html/Rat-2-3.html

should explain this.





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2007-07-18 11:58 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox