From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e5eb8ca5dcea2827 X-Google-Attributes: gid103376,public From: Richard D Riehle Subject: Re: Ada OO Mechanism Date: 1999/05/25 Message-ID: <7ifd6l$bmf@sjx-ixn1.ix.netcom.com> X-Deja-AN: 482106334 References: <7i05aq$rgl$1@news.orbitworld.net> <7i17gj$1u1k@news2.newsguy.com> <7icgkg$k4q$1@nnrp1.deja.com> <3749E9EC.2842436A@aasaa.ofe.org> <7id2eo$fag@drn.newsguy.com> <3749FF7D.F17CE16A@aasaa.ofe.org> <374AC676.F7AE0772@lmco.com> <7ieuja$5v9@news1.newsguy.com> Organization: Netcom X-NETCOM-Date: Tue May 25 4:53:57 PM PDT 1999 Newsgroups: comp.lang.ada Date: 1999-05-25T16:53:57-07:00 List-Id: In article , Hyman Rosen 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