comp.lang.ada
 help / color / mirror / Atom feed
* implicit cast and inheritance
@ 2004-10-01 20:50 Rick Santa-Cruz
  2004-10-02  0:09 ` Stephen Leake
  0 siblings, 1 reply; 8+ messages in thread
From: Rick Santa-Cruz @ 2004-10-01 20:50 UTC (permalink / raw)


Hi,

I have a small question. See the following source-code:
package Pack is
    type Base is tagged
        record
            Value: Integer;
        end record;
    procedure Base_Proc(B: Base);

    type Derived is new Base with
        null record;
end Pack;

And now I have the following source for the main-procedure:
with Pack;
use Pack;

procedure main is
    B: Base;
    D: Derived;
    procedure Proc(B: Base) is
    begin
        null;
    end Proc;
begin
    Pack.Base_Proc(B);    -- 1
    Pack.Base_Proc(D);    -- 2
    Proc(B);   -- 3
    Proc(D);    -- 4
end main;

It is clear to me, that the (4) won't compile, but I can solve it in making 
a cast or defining Proc with 'Class. My question is now why I can call 
Base_Proc(D). It is clear to me that the class Derived inherit the 
Base_Proc-Procedure from its base-class, but what is with the parameter? Why 
does this work?
I have a suggestion why this works... cause it is in the same package... but 
how does it internly work? If it is in the same package, is then always 
implicitly done a cast, or is the procedure Base_Proc in the derived class 
"Derived" automatically defined with a parameter Type "Derived"?

Thanks a lot in advance,
Rick 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-01 20:50 implicit cast and inheritance Rick Santa-Cruz
@ 2004-10-02  0:09 ` Stephen Leake
  2004-10-02 16:32   ` Rick Santa-Cruz
  0 siblings, 1 reply; 8+ messages in thread
From: Stephen Leake @ 2004-10-02  0:09 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> Hi,
> 
> I have a small question. See the following source-code:
> package Pack is
>     type Base is tagged
>         record
>             Value: Integer;
>         end record;
>     procedure Base_Proc(B: Base);
> 
>     type Derived is new Base with
>         null record;
> end Pack;
> 
> And now I have the following source for the main-procedure:
> with Pack;
> use Pack;
> 
> procedure main is
>     B: Base;
>     D: Derived;
>     procedure Proc(B: Base) is
>     begin
>         null;
>     end Proc;
> begin
>     Pack.Base_Proc(B);    -- 1
>     Pack.Base_Proc(D);    -- 2
>     Proc(B);   -- 3
>     Proc(D);    -- 4
> end main;
> 
> It is clear to me, that the (4) won't compile, but I can solve it in making 
> a cast or defining Proc with 'Class. My question is now why I can call 
> Base_Proc(D). It is clear to me that the class Derived inherit the 
> Base_Proc-Procedure from its base-class, but what is with the parameter? Why 
> does this work?

That is _exactly_ what "inherit" means: at the point where Derived is
declared, there is implicitly declared:

procedure Base_Proc (B : Derived);

So you can call it.

> I have a suggestion why this works... cause it is in the same
> package... but how does it internly work? 

In this case, there is one compiled version of the body of Base_Proc,
and the call Base_Proc (D) calls it. D is passed by reference, so the
body doesn't know or care whether it is getting a Base type or a
Derived type.

> If it is in the same package, is then always implicitly done a cast,
> or is the procedure Base_Proc in the derived class "Derived"
> automatically defined with a parameter Type "Derived"?

There is a difference between declaring a subprogram and providing an
implementation for it.

Hope this helps,

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-02  0:09 ` Stephen Leake
@ 2004-10-02 16:32   ` Rick Santa-Cruz
  2004-10-02 21:35     ` Stephen Leake
  0 siblings, 1 reply; 8+ messages in thread
From: Rick Santa-Cruz @ 2004-10-02 16:32 UTC (permalink / raw)


Hi,

>> I have a small question. See the following source-code:
>> package Pack is
>>     type Base is tagged
>>         record
>>             Value: Integer;
>>         end record;
>>     procedure Base_Proc(B: Base);
>>
>>     type Derived is new Base with
>>         null record;
>> end Pack;
>>
>> And now I have the following source for the main-procedure:
>> with Pack;
>> use Pack;
>>
>> procedure main is
>>     B: Base;
>>     D: Derived;
>>     procedure Proc(B: Base) is
>>     begin
>>         null;
>>     end Proc;
>> begin
>>     Pack.Base_Proc(B);    -- 1
>>     Pack.Base_Proc(D);    -- 2
>>     Proc(B);   -- 3
>>     Proc(D);    -- 4
>> end main;
>>
>
> In this case, there is one compiled version of the body of Base_Proc,
> and the call Base_Proc (D) calls it. D is passed by reference, so the
> body doesn't know or care whether it is getting a Base type or a
> Derived type.
So an implict cast will be done from Derived to Base, so that the body of 
the Base_Proc(B: Base) method can be called. Is this correct?
You told me, that in the package the following will be declared:
procedure Base_Proc (B : Derived);
But what's then with the body of Pack? Will there be an body of Base_Proc(B: 
Derived) which is the same as Base_Proc(B: Base),  or how does it work?

>> If it is in the same package, is then always implicitly done a cast,
>> or is the procedure Base_Proc in the derived class "Derived"
>> automatically defined with a parameter Type "Derived"?
>
> There is a difference between declaring a subprogram and providing an
> implementation for it.
Sadly, I don't understand what you mean with this.

Thanks in advance,
Rick 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-02 16:32   ` Rick Santa-Cruz
@ 2004-10-02 21:35     ` Stephen Leake
  2004-10-02 22:05       ` Rick Santa-Cruz
  0 siblings, 1 reply; 8+ messages in thread
From: Stephen Leake @ 2004-10-02 21:35 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> So an implict cast will be done from Derived to Base, so that the body of 
> the Base_Proc(B: Base) method can be called. Is this correct?

It's "type conversion" in Ada, but yes.

> You told me, that in the package the following will be declared:
> procedure Base_Proc (B : Derived); But what's then with the body of
> Pack? Will there be an body of Base_Proc(B: Derived) which is the
> same as Base_Proc(B: Base), or how does it work?

There is only one body, called for both declarations.

> >> If it is in the same package, is then always implicitly done a
> >> cast, or is the procedure Base_Proc in the derived class
> >> "Derived" automatically defined with a parameter Type "Derived"?
> >
> > There is a difference between declaring a subprogram and providing an
> > implementation for it.
> Sadly, I don't understand what you mean with this.

Just that, as above, sometimes you have two declarations of
subprograms, but only one body.

This will get more confusing with Ada 2005, which will have Java-style
interfaces :).

I suggest you buy "Ada as a Second Language" by Norm Cohen. He
explains all of these things better than I do, and you are clearly
ready for that level of book. It's available from Amazon.com, $87 new,
$30 used.

I hope he writes an update for 2005.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-02 21:35     ` Stephen Leake
@ 2004-10-02 22:05       ` Rick Santa-Cruz
  2004-10-03 10:23         ` Marius Amado Alves
  2004-10-03 12:32         ` Stephen Leake
  0 siblings, 2 replies; 8+ messages in thread
From: Rick Santa-Cruz @ 2004-10-02 22:05 UTC (permalink / raw)


Hi,

>
> There is only one body, called for both declarations.
Ok, it's clear. Thanks!

>
> I suggest you buy "Ada as a Second Language" by Norm Cohen. He
> explains all of these things better than I do, and you are clearly
> ready for that level of book. It's available from Amazon.com, $87 new,
> $30 used.
Is this book an advanced book, cause I think I first should concentrate on 
the basic things... I usually look in the book from John English, which is 
quiete good, but in my simple opinion not well structured.

Bye,
Rick 





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-02 22:05       ` Rick Santa-Cruz
@ 2004-10-03 10:23         ` Marius Amado Alves
  2004-10-03 13:27           ` CBFalconer
  2004-10-03 12:32         ` Stephen Leake
  1 sibling, 1 reply; 8+ messages in thread
From: Marius Amado Alves @ 2004-10-03 10:23 UTC (permalink / raw)
  To: comp.lang.ada

>>I suggest you buy "Ada as a Second Language" by Norm Cohen. He
>>explains all of these things better than I do, and you are clearly
>>ready for that level of book. It's available from Amazon.com, $87 new,
>>$30 used.
> 
> Is this book an advanced book, cause I think I first should concentrate on 
> the basic things... I usually look in the book from John English, which is 
> quiete good, but in my simple opinion not well structured.

Cohen's is a *complete* book. The Ada bible. To start I recomend 
Ben-Ari's Ada for Software Engineers (300 pages), or Barnes'es 
Programming in Ada (600 pages).




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-02 22:05       ` Rick Santa-Cruz
  2004-10-03 10:23         ` Marius Amado Alves
@ 2004-10-03 12:32         ` Stephen Leake
  1 sibling, 0 replies; 8+ messages in thread
From: Stephen Leake @ 2004-10-03 12:32 UTC (permalink / raw)
  To: comp.lang.ada

"Rick Santa-Cruz" <rick_santa_cruz75@msn.com> writes:

> Hi,
> 
> >
> > There is only one body, called for both declarations.
> Ok, it's clear. Thanks!
> 
> >
> > I suggest you buy "Ada as a Second Language" by Norm Cohen. He
> > explains all of these things better than I do, and you are clearly
> > ready for that level of book. It's available from Amazon.com, $87 new,
> > $30 used.
> Is this book an advanced book, 

Most people consider it "advanced", or "language lawyer" level. But
the questions you are asking indicate you need that level of book. I
read it as my _first_ Ada 95 book, after reading the Ada Language
Reference manual as my _first_ Ada 83 book. Different people have
different ways of learning!

> cause I think I first should concentrate on the basic things... I
> usually look in the book from John English, which is quiete good,
> but in my simple opinion not well structured.

Then you will find Cohen's bookd very well structured.

-- 
-- Stephe




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: implicit cast and inheritance
  2004-10-03 10:23         ` Marius Amado Alves
@ 2004-10-03 13:27           ` CBFalconer
  0 siblings, 0 replies; 8+ messages in thread
From: CBFalconer @ 2004-10-03 13:27 UTC (permalink / raw)


Marius Amado Alves wrote:
> 
... snip ...
> 
> Cohen's is a *complete* book. The Ada bible. To start I recomend
> Ben-Ari's Ada for Software Engineers (300 pages), or Barnes'es
> Programming in Ada (600 pages).

Is this the same Ben-Ari that wrote "Principles of Concurrent
Programming"?  If so, he is very readable.

-- 
Chuck F (cbfalconer@yahoo.com) (cbfalconer@worldnet.att.net)
   Available for consulting/temporary embedded and systems.
   <http://cbfalconer.home.att.net>  USE worldnet address!





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2004-10-03 13:27 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-01 20:50 implicit cast and inheritance Rick Santa-Cruz
2004-10-02  0:09 ` Stephen Leake
2004-10-02 16:32   ` Rick Santa-Cruz
2004-10-02 21:35     ` Stephen Leake
2004-10-02 22:05       ` Rick Santa-Cruz
2004-10-03 10:23         ` Marius Amado Alves
2004-10-03 13:27           ` CBFalconer
2004-10-03 12:32         ` Stephen Leake

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