comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Ada OOP question
Date: Mon, 11 Dec 2000 15:52:50 -0600
Date: 2000-12-11T15:52:50-06:00	[thread overview]
Message-ID: <Y%bZ5.1311$oK3.376302@homer.alpha.net> (raw)
In-Reply-To: wvtY5.11680$Ei1.812152@bgtnsc05-news.ops.worldnet.att.net

James S. Rogers wrote in message ...

>The question clearly comes from a person who has learned C++ or Java.
>Those languages allow classes to be defined with mixtures of public and
>private data.
>
>The same effect can be achieved in Ada, although the syntax is not as
clean
>as in C++ or Java.
>
>A tagged type in Ada is defined either in the public or private area of
a
>package specification. It is not defined partially in the public area
and
>partially in the private area. At first glance this seems to forbid
creating a
>tagged type with both public and private data members. There is a
solution.
>
>In Ada you need to extend the inheritance hierarchy a little deeper to
>achieve your goals.  Follow these steps:
>
>1) Declare a tagged type in the public part of a package specification.
>2) Extend that tagged type with a private extension in the private part
of a
>    child package specification.
>3) Inherit all future tagged types in that class from the child package
with the
>    private definition.

It's not necessary to create a separate package for the extension (IF
you're careful with the placement of primitive operations). This is an
important difference between C++ and Ada (since the encapsulation and
inheritance mechanisms are separate in Ada).

If your only purpose is to have both kinds of components, make the
original type abstract so no one can accidentally create an object of it
(without the private components):

(We use this technique in Claw to hide locks associated with some
objects...)

package Employee_Package is
   type Public_Employee is abstract tagged record -- No objects of this
are allowed.
      Name : String(1..30);
      Age    : Positive;
      Employee_Id : String(1..8);
      Title   :  String(1..20);
  end record;


  type Employee is new Public_Employee with private;

 -- Follow with public subprograms appropriate to
 -- the above tagged type definition


private
   type Employee is new Public_Employee with record
      Salary : Float;
   end record;
end Employee_Package;

>Every instance of the Employee tagged type, or any instance in its
>class hierarchy, will have both public and private data members.


            Randy Brukardt
            R.R. Software, Inc.






       reply	other threads:[~2000-12-11 21:52 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20001209101904.07653.00004016@ng-fy1.aol.com>
     [not found] ` <wvtY5.11680$Ei1.812152@bgtnsc05-news.ops.worldnet.att.net>
2000-12-11 21:52   ` Randy Brukardt [this message]
     [not found] ` <kduY5.4872$M5.273278@news1.frmt1.sfba.home.com>
     [not found]   ` <3A32A182.54E3D857@acm.org>
     [not found]     ` <91382k$nq1$1@nnrp1.deja.com>
2000-12-12  5:14       ` Ada OOP question Jeff Carter
2000-12-14 19:00         ` mark_lundquist
2000-12-14 23:05           ` Jeff Carter
2000-12-12 21:57 ` Stephen Leake
replies disabled

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