From: markus034@gmail.com
Subject: Re: Translating C++ class types into Ada tagged types?
Date: Mon, 09 Jul 2007 20:26:47 -0700
Date: 2007-07-09T20:26:47-07:00 [thread overview]
Message-ID: <1184038007.190905.237050@x35g2000prf.googlegroups.com> (raw)
In-Reply-To: <1184033524.336233.241450@d30g2000prg.googlegroups.com>
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.
next prev parent reply other threads:[~2007-07-10 3:26 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 [this message]
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
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox