TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy


Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Condense Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
"Team Ada: Ada Advocacy Issues (83 & 95)" <[log in to unmask]>
Rick Duley <[log in to unmask]>
Mon, 23 Sep 2002 11:11:30 -0400
Stephen Leake <[log in to unmask]>
Stephen Leake <[log in to unmask]>
text/plain; charset=us-ascii
text/plain (74 lines)
Rick Duley <[log in to unmask]> writes:

> Hi Teamers
> I have been working on a class assignment in object-oriented programming
> and have encountered a couple of curiosities.  The last time I did
> user-defined OO I was using Borland Pascal Seven and Ada seems to
> provide a less intuitive environment.  I think I am the last surviving
> Ada programmer in the Perth education system

It is hard being alone; welcome to the club :(.

> and, with a dearth of texts which deal with Ada OO, I am having real
> problems.

There are books on using Ada for an Object Oriented approach; see

> 1. When I create a derived class, the methods of that class have no
> direct assess to the attributes of the base class. Therefore, to be
> able to initialise an instance of the derived class I have to
> provide the functionality in the base class to access those
> attributes, which has the effect of making them public. I am certain
> that students will ask why we go to the trouble of making the tagged
> record private if we are then to go to the trouble of making the
> data public. Can someone provide me with an explanation?

You'll have to provide an example. In general, what you want to do is
possible. For example:

package Base_Class is
   type Base_Type is tagged private;
   type Base_Type is tagged record
      Attribute_1 : Some_Type;
   end record;
end Base_Class;

package Base_Class.Derived_Class is
   type Derived_Type is new Base_Type with private;

   procedure Initialize (Item : in out Derived_Type);
   --  Body of Initialize has direct visibility to Attribute_1.

   type Derived is new Base_Type with record
      Attribute_2 : Some_Other_Type;
   end record;
end Base_Class.Derived_Class;

Note that Derived_Class is a child package of Base_Class; that's why
it has visibility.

If you are trying to create a derived class in a different package,
not a child of Base_Class, then yes, the Base_Class package needs to
provide some way to properly initialize its private attributes.

The point of having private stuff is to provide abstraction. This is
only useful if Base_Class provides enough functionality (beyond simply
setting attributes) that it makes sense to hide it.

One way is with access functions, or a general Initialize.

Another way is to make some attributes public, and some private. You
can use discriminants for the public part (if they don't need to
change), or an access value to a private part (that is allocated by an
Initialize operation).

comp.lang.ada is probably a better place for this discussion.

-- Stephe