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,7137ee7358078d09 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news2.google.com!postnews.google.com!m73g2000cwd.googlegroups.com!not-for-mail From: "Ludovic Brenta" Newsgroups: comp.lang.ada Subject: Re: Basic Explaination of OO in Ada Date: 19 Sep 2006 08:31:20 -0700 Organization: http://groups.google.com Message-ID: <1158679879.965952.156540@m73g2000cwd.googlegroups.com> References: <1158593087.194781.250030@e3g2000cwe.googlegroups.com> <1158636734.971377.112550@d34g2000cwd.googlegroups.com> <1158674185.887102.205150@m73g2000cwd.googlegroups.com> NNTP-Posting-Host: 212.190.145.10 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1158679885 5246 127.0.0.1 (19 Sep 2006 15:31:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Tue, 19 Sep 2006 15:31:25 +0000 (UTC) In-Reply-To: <1158674185.887102.205150@m73g2000cwd.googlegroups.com> User-Agent: G2/1.0 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.0.7) Gecko/20060909 Firefox/1.5.0.7,gzip(gfe),gzip(gfe) X-HTTP-Via: 1.1 SEVPXS01 Complaints-To: groups-abuse@google.com Injection-Info: m73g2000cwd.googlegroups.com; posting-host=212.190.145.10; posting-account=ZjNXewwAAADyBPkwI57_UcX8yKfXWOss Xref: g2news2.google.com comp.lang.ada:6668 Date: 2006-09-19T08:31:20-07:00 List-Id: > So basically, OO is the principles: > polymorph, > encaps, > inheritance. > > And Programming by extension is the way to do it in Ada, as > class something: > { > } > would be in C++? Not exactly. That construct in C++ corresponds to several of Ada's constructs: - a class in C++ is a unit of encapsulation, since it has public, private and protected parts. In Ada, packages, not types, are the units of encapsulation; they have public, body and private parts (respectively). - a class T in C++ corresponds to a tagged type in Ada, but it also implicitly declares a pointer type (T*) and a reference type (T&) which correspond to Ada's class-wide access types; thus: class T {}; is equivalent to package P is type T is tagged null record; type Pointer_To_T is access all T'Class; type Reference_To_T is access all T'Class; end P; Programming by extension does not necessarily involve inheritance. Consider: package Pak2 is type T is private; -- not necessarily tagged procedure Proc (Object : in T); private type T is ... end Pak2; package Pak2.Extensions is procedure Additional_Operation (Object : in out T); type Extended is record Base : T; Additional_Data : ...; end record; end Pak2.Extensions; This is extension by composition, as opposed to inheritance. Of course, you already understand how to program by extension by means of inheritance. there are still other ways, like "mix-ins", which combines composition and inheritance. > One thing I'm still confused on is the use of access objects > versus/with the use of object'class. > I understand using object'class for polymorphism and inheritance, but > can access objects be used in a similar manner? I think about it this way. An object of a class-wide type is indefinite; we do not know its size, because its specific type may be anywhere in the inheritance tree. In contrast, an access-to-class-wide-object has a known size (usually just that of an address). As a consequence, you can create arrays of access values, but not arrays of class-wide objects. So, access types are not necessary to achieve polymorphism; the following achieves polymorphism without an access type: package Pak is type T is tagged private; procedure P (Object : in out T); -- primitive operation end Pak; -- body omitted with Pak; procedure Test (Object : in out Pak.T'Class) is begin Pak.P (Object); -- dynamic dispatch, achieves polymorphism end Test; Access types come in handy if you want a collection of class-wide objects; the collection is said to be polymorphic: with Pak; package Collection is type Ref is access Pak.T'Class; -- access-to-class-wide-objects type Vector is array (Positive range <>) of Ref; -- polymorphic vector procedure Traverse (V : in Vector); end Collection; package body Collection is procedure Traverse (V : in Vector) is begin for J in V'Range loop if V (J) /= null then Pak.P (V (J).all); -- dispatches on the specific type of the object end if; end loop; end Traverse; end Collection; Here's another interesting tip for C++ programmers. In C++, once you declare a member function to be "virtual", all calls to that member function dispatch dynamically, across the entire program. Conversely, if a member function lacks the "virtual" keyword, calls to it are always static. The only way you can determine whether calls dispatch statically or dynamically is by looking at the declaration of the member function. Things can become quite nasty if a class overrides an inherited, non-virtual member function, and makes it virtual. I'm not even sure what the language rules are in this case. In contrast, in Ada, you do not declare primitive operations as "virtual", or "dynamic" or whatever. Instead, you choose *at the point of call* whether or not the call dispatches. Consider: with Pak; use Pak; procedure Test (Object : in out T'Class) is begin P (Object); -- dynamic dispatch P (T (Object)); -- view conversion to a specific type => static dispatch end Test; As consequences: - you always know whether or not the call dispatches dynamically by just looking at the call - you can choose how to dispatch based on your requirements - a primitive operation cannot be "virtual" in some contexts and "non-virtual" in others. It is always "potentially virtual". HTH -- Ludovic Brenta.