comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <laoXhai@ix.netcom.com>
Subject: Re: Ada OO Mechanism
Date: 1999/05/25
Date: 1999-05-25T16:53:57-07:00	[thread overview]
Message-ID: <7ifd6l$bmf@sjx-ixn1.ix.netcom.com> (raw)
In-Reply-To: t7675gswiv.fsf@calumny.jyacc.com

In article <t7675gswiv.fsf@calumny.jyacc.com>,
	Hyman Rosen <hymie@prolifics.com> wrote:

>I wanted it to sound somewhat contentious. The named people each
>claimed in a posting that there were object-oriented techniques
>that were harder, or less elegant, in C++ than in Ada. I asked
>for examples with the thought that perhaps the posters didn't know
>C++ as well as they knew Ada, and were therefore perhaps mistaken.
>There were responses, but no code, so I got a little testy.

OK.  There are hundreds of source code examples one could choose.  I
promised myself not to get into this any further, but it
just isn't to be. I am going to give just this one example illustrating
array management using Ada with the suggestion that Mr. Rosen post
some corresponding code in C++.  My apologies to those of you in the
Ada community who prefer to avoid these bladder-purging contests.

In C++ every array begins with an index of zero.  This makes 
array algorithms slightly more awkward than Ada.  Ada does not
assume that everything in the world begins with zero.  In C++,
this often results in "off by one" errors, all too easy to create,
all too difficult to debug in large programs. Of course, no 
intelligent programmer would make this mistake. ;)


Also, multiple dimension arrays in Ada may be declared as actual multiple 
dimension arrays as well as arrays of arrays.  I assume you know
C++ well, so will not bother to code each example in C++ except
where necessary.  Here are some Ada examples that are useful.
 
 Example One:

     type Float_32 is digits 6;  -- declare 32 bit float;
     for Float_32'Size use 32;   -- force size to 32 bits
     type Index is range -42..42;
     -- a constrained array type
     type Life_Universe_And_Everything is array(Index) of Float_32;
     Babel_Fish : Life_The_Universe_And_Everything;
  
     Now we might want to traverse this array from beginning to end. 
     Ada makes this quite easy.  Consider,

              for I in Babel_Fish'Range
                  loop
                     -- some computation
                  end loop;
 
     That is readable Ada.  Since you are the C++ expert, we leave
     it to you to supply a corresponding loop in C++, one that 
     processes an array with a negative index. 

 Example 2:

     Ada will permit us to declare an unconstrained array in which
     the index may be of any discrete type.  The index does not even
     have to be an integer.  We will, however, use an integer type
     type in this example.

     type My_Index is range - 2**12..2**12-1:  -- (-4096 .. 4095);
     type FourKarray is array(My_Index <>) of Float_32;

     The array is an unconstrained array.  Before we may use it, it
     must be constrained.  We may also call a function or procedure
     with an unconstained array and generalize the code quite nicely.

     
        The_Array : FourKarray(-3000..2000);
     
        procedure Process(Data : in out FourKarray) is   
        begin
           for I in F'Range 
              loop
                -- actions on array, F;
               end loop;
        end Process;

     We might call Process with X,

        Process(Data => The_Array); -- associate formal with actual

     Once again, you might supply C++ code for this algorithm and 
     the corresponding procedure call.

 Example 3:

     As mentioned earlier, Ada permits many techniques for 
     declaring and traversing multiple dimensional arrays.
     Here is an example that you might find interesting.

     type Month is (Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept,
                    Oct, Nov, Dec);
     type Day is range 1..31;

     type Day_Of_Month is array(Month, Day) of Boolean;

     Now we want to traverse the array with Month as the major
     index of our loop.

           for I in Day_Of_Month'Range(1)
                loop
                   for J in Day_Of_Month'Range(2)
                       loop
                           -- Do something to element of matrix
                           if some-condition then
                             Day_Of_Month(I, J) := True;
                           end if;
                       end loop;
                end loop;
     
        Notice that the enumerated type is an ordered set, unlike
        C++.  Also, there is no need to do any arithmetic on the
        enumerated type.  In the example shown, we are permitted
        to identify which dimension of the array is being processed
        by putting it in parentheses. Therefore, 'Range(2) refers
        to the second dimension.  This is handy if I decide to do 
        it differently.  Consider,

                for I in Day_Of_Month'Range(2)
                loop
                   for J in Day_Of_Month'Range(1)
                       loop
                           -- Do something to element of matrix
                           if some-condition then
                             Day_Of_Month(I, J) := True;
                           end if;
                       end loop;
                end loop;

   where we change the major and minor dimensions through a simple
   alteration of the 'Range(X) attribute.  Once again, you are
   challenged to provide corresponding code in C++.  

   I could many more more examples of array processing in 
   Ada that are simple, straightforward and easy to code.  One more
   important point;  the index to the array is a constant and only
   lives as long as the scope of the array.  In the current C++
   standard, the index also goes out of scope, but this was not always
   the case.  Some C++ compilers do not yet apply the current standard,
   and some permit the older code to still compile in the interest
   portability -- but at the expense of reliability.  More important,
   in C++ one can code a loop such as,

              for (i = 0; i < 10; i++)
                { // some code here
                     i = 10;
                }

which would be illegal in Ada for safey reasons.  In general, array and
matrix processing in Ada is so much superior to C++ that it makes 
me wonder why anyone would even raise a question about it.   It is not
unknown, in C++ programs, to have defects that result from simple
programming errors.  For example, has anyone ever coded i++ when they
intended ++i?  Will it make a difference in the behavior of the program?
Will the compiler even bother to give you a warning?  

On the other hand, we could find some illustrations where C++ does
certain things better than Ada.  That problme is left as an exercise
to the student.

Richard Riehle
richard@adaworks.com
http://www.adaworks.com

   
 
     
     
 




  parent reply	other threads:[~1999-05-25  0:00 UTC|newest]

Thread overview: 116+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-05-20  0:00 Ada OO Mechanism Shawn M. Root
1999-05-20  0:00 ` Samuel Mize
1999-05-20  0:00   ` David Botton
1999-05-20  0:00     ` Samuel Mize
1999-05-20  0:00       ` David Botton
1999-05-24  0:00   ` Hyman Rosen
1999-05-24  0:00     ` Robert Dewar
1999-05-24  0:00       ` Hyman Rosen
1999-05-24  0:00         ` Mike
1999-05-25  0:00           ` Robert Dewar
1999-05-24  0:00         ` David Starner
1999-05-24  0:00           ` bob
1999-05-24  0:00             ` David Starner
1999-05-25  0:00               ` Ole-Hjalmar Kristensen
1999-05-25  0:00                 ` Florian Weimer
1999-05-25  0:00                 ` Mark A Biggar
1999-05-25  0:00                   ` Hyman Rosen
1999-05-25  0:00                     ` Richard D Riehle
1999-05-25  0:00                       ` David Botton
1999-05-26  0:00                         ` Tom Moran
1999-05-27  0:00                       ` Aidan Skinner
1999-05-25  0:00                     ` Samuel Mize
1999-05-25  0:00                       ` Hyman Rosen
1999-05-25  0:00                         ` Brian Rogoff
1999-05-25  0:00                           ` Jim
1999-05-26  0:00                           ` Robert Dewar
1999-05-26  0:00                             ` Brian Rogoff
1999-05-25  0:00                         ` Samuel Mize
1999-05-25  0:00                           ` Chris
1999-05-25  0:00                             ` David Botton
1999-05-27  0:00                               ` Aidan Skinner
1999-05-27  0:00                                 ` Gautier
1999-05-27  0:00                             ` Samuel Mize
1999-05-25  0:00                         ` Richard D Riehle [this message]
1999-05-25  0:00                           ` Hyman Rosen
1999-05-26  0:00                             ` Ray Blaak
1999-05-26  0:00                               ` Hyman Rosen
1999-05-26  0:00                               ` Richard D Riehle
1999-05-26  0:00                                 ` Hyman Rosen
1999-05-27  0:00                                   ` Richard D Riehle
1999-06-05  0:00                                     ` Matthew Heaney
1999-06-07  0:00                                       ` Hyman Rosen
1999-05-28  0:00                                   ` Laurent Guerby
1999-06-05  0:00                                   ` Matthew Heaney
1999-06-07  0:00                                     ` Hyman Rosen
1999-06-08  0:00                                       ` Matthew Heaney
1999-06-08  0:00                                         ` Hyman Rosen
1999-06-08  0:00                                           ` Samuel Mize
1999-06-08  0:00                                             ` Hyman Rosen
1999-06-08  0:00                                       ` Robert Dewar
1999-06-08  0:00                                         ` Stanley R. Allen
1999-06-08  0:00                                         ` Markus Kuhn
1999-06-08  0:00                                           ` Stanley R. Allen
     [not found]                           ` <t7zp2sr6yf.fsf@calumny.jyacc.c <t7emjmmx8w.fsf@calumny.jyacc.com>
1999-06-08  0:00                             ` Larry Kilgallen
1999-06-08  0:00                               ` Hyman Rosen
1999-06-08  0:00                                 ` Tucker Taft
1999-06-08  0:00                                   ` Brian Rogoff
1999-06-09  0:00                                   ` Robert Dewar
     [not found]                                   ` < <375E92CB.27850620@averstar.com>
1999-06-09  0:00                                     ` Brian Rogoff
1999-06-14  0:00                                       ` Robert A Duff
1999-06-09  0:00                                   ` Tucker Taft
1999-06-09  0:00                                 ` Matthew Heaney
1999-06-09  0:00                                 ` Samuel Mize
     [not found]                           ` <t7zp2sr6yf.fsf@calumny.jyacc.c <t7r9nmz8ou.fsf@calumny.jyacc.com>
1999-06-08  0:00                             ` Larry Kilgallen
1999-06-08  0:00                               ` Hyman Rosen
1999-06-14  0:00                                 ` Robert A Duff
     [not found]                           ` <t7zp2sr6yf.fsf@calumny.jyacc.c <375d9a3d.e1cccc63@averstar.com>
1999-06-09  0:00                             ` Larry Kilgallen
1999-06-09  0:00                               ` Tucker Taft
1999-05-27  0:00                         ` Samuel Mize
1999-05-27  0:00                           ` Jon S Anthony
1999-05-27  0:00                         ` Samuel Mize
1999-05-27  0:00                           ` Hyman Rosen
1999-05-28  0:00                             ` Samuel Mize
1999-05-28  0:00                             ` Laurent Guerby
1999-05-28  0:00                               ` Richard D Riehle
1999-05-28  0:00                                 ` Tom Moran
1999-05-28  0:00                     ` Robert I. Eachus
1999-05-28  0:00                       ` Brian Rogoff
1999-05-29  0:00                       ` Ehud Lamm
1999-05-30  0:00                         ` chris
1999-05-30  0:00                           ` Harry George
1999-05-30  0:00                             ` Vladimir Olensky
1999-05-31  0:00                               ` Robert Dewar
1999-05-30  0:00                           ` Robert Dewar
1999-05-31  0:00                           ` Vladimir Olensky
1999-06-03  0:00                             ` Dale Stanbrough
1999-06-02  0:00                               ` mike
1999-06-03  0:00                                 ` Robert Dewar
1999-06-06  0:00                                   ` David Botton
1999-06-07  0:00                                     ` Robert Dewar
1999-06-01  0:00                           ` Richard D Riehle
1999-06-03  0:00                         ` Matthew Heaney
1999-06-03  0:00                     ` Matthew Heaney
1999-05-25  0:00     ` Samuel Mize
1999-05-25  0:00       ` Hyman Rosen
1999-05-25  0:00         ` David Starner
1999-05-26  0:00         ` Laurent Guerby
1999-05-26  0:00           ` Hyman Rosen
1999-05-28  0:00             ` Laurent Guerby
1999-06-01  0:00               ` Hyman Rosen
1999-06-03  0:00                 ` Fraser Wilson
1999-05-26  0:00         ` Ole-Hjalmar Kristensen
1999-06-03  0:00     ` Matthew Heaney
1999-06-03  0:00       ` Hyman Rosen
1999-05-21  0:00 ` Dale Stanbrough
1999-05-20  0:00   ` bob
1999-05-21  0:00     ` Dale Stanbrough
1999-05-21  0:00   ` Richard D Riehle
1999-05-21  0:00     ` Marin David Condic
1999-05-21  0:00       ` Steve
1999-05-21  0:00       ` Dan Nagle
1999-05-24  0:00         ` Marin David Condic
1999-05-21  0:00     ` Shawn M. Root
1999-05-21  0:00       ` Richard D Riehle
1999-05-25  0:00         ` Shawn M. Root
1999-05-25  0:00   ` Don Overheu
replies disabled

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