comp.lang.ada
 help / color / mirror / Atom feed
From: dodger@gcr.com (Roger Labbe)
Subject: Re: don't understand packages vs. objects/classes
Date: 27 Nov 1994 09:58:38 -0500
Date: 1994-11-27T09:58:38-05:00	[thread overview]
Message-ID: <3ba6qu$6rs@mandolin.gcr.com> (raw)
In-Reply-To: Czwt26.G0B@seas.ucla.edu

As declared, you cannot have two or more stacks. The key is to define
a data type called stack and encapsulate it in a package (which I'll
call STACKS in a failure of creativity). THus you might have:

-- Lame implementation without error handling or other necessary stuff.
generic
   type element is private;
package STACKS is

   type stack is private;

   procedure push (s     : stack;
                   value : element);

   ... rest of spec
end STACKS;

Then in your procedure you can declare
 
package int_stack is new stacks (element => integer);
package float_stack is new stacks (element => your_float);

   x : int_stack;
   y, z : float_stack;
begin
   push(x, 3);


Now whether you want to consider this library based or object based
is up to you. But how different is the C++ equivalent

   stack<int> x;

   x.push(3);

The x is outside the push instead of a parameter as in Ada, but I consider
the code equivalent (as far as this example goes).

As far as class hierarchies go Ada 83 doesn't really have them. You can use
derived types to inherit operations and add operations, but you cannot
add new components. Briefly, Ada 94 allows tagged types which may be
extended by derivation to add new components. This is a topic that is
too complex for a single post, and probably not to important to someone
just learning the language. But yes, packages in Ada 83 are limited; they
are better at implementing encapsulation than object oriented features.

Get the Ada 9x rationale from the Ada web server if you want to read
a good introduction to Ada 94 and object oriented programming. I don't
have the address right here, but you can find it easily in the newsgroup;
alternatively, send me email and I'll send it to you.

Roger Labbe




  reply	other threads:[~1994-11-27 14:58 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-11-27  4:42 don't understand packages vs. objects/classes Andrew E. Caldwell
1994-11-27 14:58 ` Roger Labbe [this message]
1994-11-28 11:35   ` David Weller
1994-11-28 15:02 ` Mark C. Chu-Carroll
  -- strict thread matches above, loose matches on Subject: below --
1994-11-29 17:04 Bennett, Chip (KTR) ~U
replies disabled

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