comp.lang.ada
 help / color / mirror / Atom feed
From: Charles Hixson <charleshixsn@earthlink.net>
Subject: Re: What is a class in Ada ?
Date: 2000/02/17
Date: 2000-02-17T00:00:00+00:00	[thread overview]
Message-ID: <38AC393F.A485E77A@earthlink.net> (raw)
In-Reply-To: gUWo4.577$zE2.20127@newsread1.prod.itd.earthlink.net

Thank you!
That's the best explanation of a class in Ada that I've run across.  The only thing
that feels left out is that in Ada there is never a presumption that the first
argument of a function is the class "Of which it is the method", as methods are
allowed to be defined separately.  This makes the language more flexible at the cost
of being a bit more verbose.

Thus methods are "owned" by modules, not by classes.  Different modules can use the
same method name in different ways, as in Java or C++ different classes can use the
same method name in different ways.  I believe, but I've never tested, that within
one module the same name can be used in different ways **PROVIDED ** the argument
list differs in the types of its arguments.
E.g.:

Package P is
function   random  return float;
function   random (min : Integer; max : Integer)   return Integer;
end P;


Matthew Heaney wrote:

> In article <38A42642.ADC205BD@interact.net.au> , G <gmw@interact.net.au>
> wrote:
>
> > What is a class in Ada ?
>
> There is a slight terminology difference between Ada and other
> languages, so you have to be clear when you answer the question.
>
> A type denotes a set of values and a set of operations.  In Ada, such a
> type is called "type," and in other languages it's called a "class."
>
> In Ada, a "class" denotes a family of types.  You have the "class of
> integer types," and the "class of floating point types."
>
> In Ada, you can say T'Class, which names the family of tagged types that
> have T as their ancestor.
>
> This is nice, because it lets you state explicitly whether you're
> applying an operation to any type in the class (T'Class) or to just a
> specific type (T).
>
> In other languages, as C++ and Java, this lexical distinction is not
> possible.
>
> > Does Ada have a "Class" facility in the same sense that C++ or Java have
> > such ?
>
> Yes.  A "tagged type" in Ada is equivalent to a "class" in C++ or Java.
>
> Ada also has "abstract data types" which aren't (publicly) tagged, which
> support inheritance of operations, but not type extension.
>
> > Tagged records appeared to me at first to be pretty close to the class notion.
> > There are problems though because there are no member functions in records -
> > they approximate to C/C++ structs (yes ?).
>
> A tagged record is the same as a class in C++ (or Java).
>
> There is a slight difference, however, because in Ada "type" and
> "module" are orthogonal language features.  The primitive operations
> ("member functions") for a tagged type (record) aren't declared in the
> record itself.  Rather, they are declared in the same module in which
> the record is declared.
>
> The Ada type:
>
> package P is
>
>   type T is tagged record
>     I : Integer;
>   end record;
>
>   procedure Op (O : in out T);
>
> end P;
>
> is exactly equivalent to the C++ class:
>
> class T {
> public:
>   int I;
>   void Op;
> };
>
> (I'm not a C++ programmer, so my little example may not be quite
> correct.)
>
> > As I understand it, these are Abstract Data Types - and not in themselves
> > dynamic objects.
>
> I don't know what you mean by "dynamic object."
>
> Really they're all abstract data types, it's just that some of them are
> tagged (and therefore extensible) and some aren't tagged.  For example:
>
> package P is
>
>   type Nontagged_Type is private;
>   procedure Op (O : in out Nontagged_Type);
>
>   type Tagged_Type is tagged private;
>   procedure Op (O : in out Tagged_Type);
> ...
> end P;
>
> Really both of these are abstract data types.  The difference shows up
> during a derivation:
>
> package P.C is
>
>   type Derives_From_Nontagged_Type is new Nontagged_Type;
>
>   type Derives_From_Tagged_Type is new Tagged_Type with record
>     F : Float;
>   end record;
> ...
> end P.C;
>
> You're allowed to extend Tagged_Type with a new component (here,
> component F of type Float).  It is not possible to extend
> Nontagged_Type.
>
> Of course, in both cases, primitive operations are inherited.  Here,
> both derived types come with a primitive operation called Op.
>
> > In C++, the definition of a class appears fairly transparent and not much more
> > difficult than creating an instance of a record in Ada.  I have not found
> > anything in my html tutes or paper books to tell me more about "Objects" in
> > Ada.  Yet.
>
> A "class" in C++ is equivalent to a "tagged record" in Ada.
>
> In Ada, an "object" is an instance of any type:
>
> Integer_Object : Integer;
> Float_Object : Float;
> Object_Whose_Type_Is_Tagged : Tagged_Type;
>
> These are all "objects" in Ada.
>
> > So - are the packages of Ada the class definitions, maybe  ?
>
> Sort of.  It's hard to compare languages here, because in Ada, module
> are type are orthogonal, which is not the case in other languages.
>
> The purpose of a package (module) in Ada is to demarcate, from among all
> the operations in the universe that take type T as a parameter, those
> operations that are "primitive" for the type.
>
> We care about primitiveness, because only primitive operations are
> inherited during a derivation.
>
> For example,
>
> package P is
>
>   type T is private;  -- tagged-ness doesn't matter here
>
>   procedure Op1 (O : in out T);
> ...
> end P;
>
> with P;
> package Q is
>
>   procedure Op2 (O : in out P.T);
>
> end Q;
>
> Operation P.Op1 is primitive for type T, and therefore it gets inherited
> during a derivation.  For example:
>
> package P.C is
>
>   type NT is new T;
>
> end P.C;
>
> Although it's not obvious from the text of package P.C, type NT does
> have an operation called Op1, and you can legally do this:
>
> declare
>   O : P.C.NT;
> begin
>   P.C.Op1 (O);
> end;
>
> However, operation Q.Op2 is not primitive for type P.T, because it's not
> declared in the same package as package in which T is declared (here,
> P).  Therefore it is not inherited.
>
> So you can do this:
>
> declare
>   O : P.T;
> begin
>   Q.Op2 (O);
> end;
>
> but you cannot do this:
>
> declare
>   O : P.C.NT;
> begin
>   Q.Op2 (O);  -- illegal
> end;
>
> > I am still learning, excuse my simplicity.
>
> The only dumb question is the question that doesn't get asked.
>
> Hope that helps some,
> Matt
> --
> Celebrate Darwin Day on Saturday, 12 February!
>
> <http://www.bbc.co.uk/education/darwin/index.shtml>





      parent reply	other threads:[~2000-02-17  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-12  0:00 What is a class in Ada ? G
2000-02-11  0:00 ` Matthew Heaney
2000-02-13  0:00   ` Martin Dowie
2000-02-14  0:00     ` Robert A Duff
2000-02-14  0:00     ` Larry Kilgallen
2000-02-14  0:00     ` Gautier
2000-02-17  0:00   ` Charles Hixson [this message]
replies disabled

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