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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham 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!d30g2000prg.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada Subject: Re: Translating C++ class types into Ada tagged types? Date: Mon, 09 Jul 2007 19:12:04 -0700 Organization: http://groups.google.com Message-ID: <1184033524.336233.241450@d30g2000prg.googlegroups.com> References: <1184031587.650366.207550@m37g2000prh.googlegroups.com> NNTP-Posting-Host: 75.70.221.169 Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" X-Trace: posting.google.com 1184033525 24266 127.0.0.1 (10 Jul 2007 02:12:05 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 10 Jul 2007 02:12:05 +0000 (UTC) In-Reply-To: <1184031587.650366.207550@m37g2000prh.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; 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: d30g2000prg.googlegroups.com; posting-host=75.70.221.169; posting-account=SqOfxAwAAAAkL81YAPGH1JdBwpUXw9ZG Xref: g2news1.google.com comp.lang.ada:16445 Date: 2007-07-09T19:12:04-07:00 List-Id: 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 // 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 // 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