comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@comcast.net>
Subject: Re: Inherited Methods and such
Date: Mon, 17 Sep 2007 21:03:45 -0700
Date: 2007-09-17T21:03:45-07:00	[thread overview]
Message-ID: <PZmdndGHf--a03LbnZ2dnUVZ_hudnZ2d@comcast.com> (raw)
In-Reply-To: 1190039166.449906.15070@g4g2000hsf.googlegroups.com

<shaunpatterson@gmail.com> wrote in message 
news:1190039166.449906.15070@g4g2000hsf.googlegroups.com...
> Hi,
>
[snip]
> Is this type of method inheritance supported in ada? If so,
> where am I going wrong?
>

Yes this type of method inheritance is supported in Ada, but you're using 
the wrong constructs.

The Ada equivalent to a C++ class is a tagged type.

In C++ you may declare a class:

class Base is
{
    public:
        virtual void Test1();
};

In Ada the equivalent is::

  type Base is tagged null record;

  procedure Test1( obj : Base );


To derive a subclass from Base in C++ you would use:

class Derived : public Base
{
    public:
        virtual void Test1();
};

The Ada equivalent is:

  type Derived is new Base with null record;

  procedure Test1( obj : Derived );

In C++ to declare an objects of type Base an Derived and to invoke their 
Test1 functions you use:

  Base obj1;
  Derived obj2;

  obj1.Test1();
  obj2.Test1();

The Ada 95 equivalent is:

  declare
    obj1 : Base;
    obj2 : Derived;
  begin
    Test1( obj1 );
    Test1( obj2 );
  end;

Or in Ada 2005 you may use the alternative syntax:


  declare
    obj1 : Base;
    obj2 : Derived;
  begin
    obj1.Test1;
    obj2.Test1;
  end;

The sample code below does some of what your example code was doing, and may 
be helpful in helping to sort things out.

-------------------------------
package Base_Package is

   type Base_Class is tagged null record;

   procedure Test1( base : Base_Class );

   procedure Test2( base : Base_Class );

end Base_Package;

-------------------------------
with Ada.Text_IO;
 use Ada.Text_IO;
package body Base_Package is

   procedure Test1( base : Base_Class ) is
   begin
     Put_Line( "Test1 FROM BASE CLASS");
   end Test1;

   procedure Test2( base : Base_Class ) is
   begin
     Put_Line( "Test2 FROM BASE CLASS");
   end Test2;

end Base_Package;

-------------------------------
with Base_Package;
 use Base_Package;
package Base_Subclass_Package is

   type Base_Subclass is new Base_Class with null record;

   procedure Test1( base : Base_Subclass );

   procedure Test2( base : Base_Subclass );

end Base_Subclass_Package;

-------------------------------
with Ada.Text_IO;
 use Ada.Text_IO;
package body Base_Subclass_Package is

   procedure Test1( base : Base_Subclass ) is
   begin
     Put_Line( "Test1 FROM BASE SUBCLASS");
   end Test1;

   procedure Test2( base : Base_Subclass ) is
   begin
     Put_Line( "Test2 FROM BASE SUBCLASS");
   end Test2;

end Base_Subclass_Package;

-------------------------------
with Base_Package;
 use Base_Package;
with Base_Subclass_Package;
 use Base_Subclass_Package;

procedure Run_Test is
   a : Base_Class;
   b : Base_Subclass;

   procedure DoIt( base : Base_Class'class ) is
   begin
     Test1( base );
   end DoIt;

begin
  DoIt( a );
  DoIt( b );
end Run_Test;

Regards,
Steve
(The Duck)

> Thanks
> --
> Shaun Patterson
> 





      parent reply	other threads:[~2007-09-18  4:03 UTC|newest]

Thread overview: 74+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-17 14:26 Inherited Methods and such shaunpatterson
2007-09-17 15:11 ` Ludovic Brenta
2007-09-17 16:46   ` shaunpatterson
2007-09-17 19:07     ` Ludovic Brenta
2007-09-17 20:22   ` Maciej Sobczak
2007-09-17 21:07     ` Ludovic Brenta
2007-09-18 14:27       ` Maciej Sobczak
2007-09-18 14:27       ` Maciej Sobczak
2007-09-18 15:25         ` Dmitry A. Kazakov
2007-09-18 18:34           ` Ludovic Brenta
2007-09-18 19:29             ` Dmitry A. Kazakov
2007-09-18 19:39               ` Ludovic Brenta
2007-09-18 20:49                 ` Dmitry A. Kazakov
2007-09-18 21:10               ` Simon Wright
2007-09-18 20:39           ` Maciej Sobczak
2007-09-18 21:12             ` Dmitry A. Kazakov
2007-09-19 14:49               ` Maciej Sobczak
2007-09-19 15:16                 ` Dmitry A. Kazakov
2007-09-19 22:13                   ` Maciej Sobczak
2007-09-20  8:12                     ` Dmitry A. Kazakov
2007-09-20 13:52                       ` Maciej Sobczak
2007-09-20 16:22                         ` Dmitry A. Kazakov
2007-09-20 20:45                           ` Maciej Sobczak
2007-09-21 18:59                             ` Dmitry A. Kazakov
2007-09-21 21:02                               ` Maciej Sobczak
2007-09-22  8:48                                 ` Dmitry A. Kazakov
2007-09-22 21:53                                   ` Maciej Sobczak
2007-09-23  8:41                                     ` Dmitry A. Kazakov
2007-09-23 20:36                                       ` Maciej Sobczak
2007-09-24  9:32                                         ` Dmitry A. Kazakov
2007-09-24 15:02                                           ` Maciej Sobczak
2007-09-24 19:20                                             ` Dmitry A. Kazakov
2007-09-25 20:53                                               ` Maciej Sobczak
2007-09-26 10:42                                                 ` Dmitry A. Kazakov
2007-09-26 21:31                                                   ` Maciej Sobczak
2007-09-27 15:02                                                     ` Dmitry A. Kazakov
2007-09-27 21:02                                                       ` Maciej Sobczak
2007-09-26 12:21                                                 ` Robert A Duff
2007-09-26 12:54                                                   ` Dmitry A. Kazakov
2007-09-26 21:37                                                   ` Maciej Sobczak
2007-09-26 23:47                                                     ` Randy Brukardt
2007-09-27 21:08                                                       ` Maciej Sobczak
2007-09-28  0:44                                                         ` Randy Brukardt
2007-09-28 20:32                                                           ` Maciej Sobczak
2007-09-28 22:35                                                             ` Randy Brukardt
2007-09-29 23:58                                                             ` Robert A Duff
2007-09-26 12:26                                                 ` Robert A Duff
2007-09-26 21:50                                                   ` Maciej Sobczak
2007-09-26 22:20                                                     ` Ray Blaak
2007-09-27  0:01                                                     ` Randy Brukardt
2007-09-27 13:39                                                     ` Robert A Duff
2007-09-27 14:54                                                       ` Dmitry A. Kazakov
2007-09-28  0:35                                                         ` Randy Brukardt
     [not found]                                                           ` <7p6gc1s9imfa$.kmvwf5zyf8e9.dlg@40tude.net>
2007-09-28 22:53                                                             ` Randy Brukardt
2007-09-29 20:37                                                               ` Dmitry A. Kazakov
2007-09-27 21:23                                                       ` Maciej Sobczak
2007-09-28 19:12                                                         ` Robert A Duff
2007-09-28 19:02                                                     ` Robert A Duff
2007-09-28 19:42                                                       ` Robert A Duff
2007-09-28 20:44                                                         ` Maciej Sobczak
2007-09-28 22:40                                                           ` Randy Brukardt
2007-09-29 20:35                                                           ` Dmitry A. Kazakov
2007-09-29 20:52                                                             ` Maciej Sobczak
2007-09-30  8:38                                                               ` Dmitry A. Kazakov
2007-09-29 23:47                                                             ` Robert A Duff
2007-09-29 20:48                                                           ` Maciej Sobczak
2007-09-29 23:39                                                             ` Robert A Duff
2007-09-30  8:38                                                               ` Dmitry A. Kazakov
2007-09-29 23:42                                                           ` Robert A Duff
2007-09-25  1:59                                   ` Randy Brukardt
2007-09-25  8:59                                     ` Dmitry A. Kazakov
2007-09-25 21:02                                       ` Randy Brukardt
2007-09-26 12:42                                         ` Dmitry A. Kazakov
2007-09-18  4:03 ` Steve [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