comp.lang.ada
 help / color / mirror / Atom feed
From: Richard Riehle <richard@adaworks.com>
Subject: Re: Problems with tagged records and inheritance
Date: Mon, 14 Jul 2003 18:34:06 -0700
Date: 2003-07-15T01:30:40+00:00	[thread overview]
Message-ID: <3F135A0E.A46C33D1@adaworks.com> (raw)
In-Reply-To: bekkpr$12eg$1@ulysses.noc.ntua.gr

I am including your posting at the end of this reply.

First, you have a visibility problem because type Base
is private and its contents are not visible.   Also, you have
a design problem.   Consider,

       package The_Base is
           type Base is tagged private;
           procedure Put(B : Base);
       private
          type Base is tagged record
                AAA :  Integer :=  42;
          end record;
       end Base;

Now you have a design with a primitive operation for Base.  You
would need more of these, of course.

      package Derived_From_Base is
           type Derived is new The_Base.Base with private;
           procedure Put(D : Derived);
       private
          type Derived is new The_Base.Base with record
                BBB :  Integer :=  451;
          end record;
      end Derived_From_Base;

Now you have a derived type  along with a primitive Put
method for it.   You implement the Put in terms of the existing
Put in The_Base.

     with Ada.Integer_Text_IO;
     package body Derived_From_Base is
      procedure Put(D : Derived) is
      begin
          The_Base.Put(The_Base.Base(D));
          Ada.Integer_Text_IO.Put(BBB);
      end Put;
    end Derived_From_Base;

One of the key ideas behind object-oriented programming
is that of reuse.   In the Put method for Derived you are
first reusing the implementation of the Put for its parent and
then coding the part for the immediately visible type.

Hope this helps.  I did not compile this so there might be some typos,
but the fundamental idea is there.

Richard Riehle

Papastefanos Serafeim wrote:

> I have using a base type like this:
> ...
> type Base is tagged private;
> ...
> type Base is tagged record
>          AAA: Integer:=1;
>       end record;
>
> and a child type like this
>
> type Child is new Base with private;
> ....
> type Child is new Base with record
>         BBB: Integer:=5;
>       end record;
>
> The problem is that the following is not working:
>
>    procedure Test(Ch: in Child) is
>    begin
>       Put(Ch.AAA); --<- This line has an error, it says no selector AAA for
> type Child
>       Put(Ch.BBB);
>    end Test;
> The procedure Test is declared in the same package
> as the type Child and defined at the package's Body.
>
> The error is becouse AAA is not part of Child.
> Why is that ? I thought that Child would contain
> AAA and BBB, and not only BBB...
>
> --
> Papastefanos Serafeim
> serafeim@otenet.gr







      parent reply	other threads:[~2003-07-15  1:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-07-10 20:56 Problems with tagged records and inheritance Papastefanos Serafeim
2003-07-10 22:15 ` Re; " tmoran
2003-07-10 22:24 ` Robert I. Eachus
2003-07-10 22:29 ` Ludovic Brenta
2003-07-11 11:56   ` Marin David Condic
2003-07-11 11:49 ` Papastefanos Serafeim
2003-07-15  1:34 ` Richard Riehle [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