comp.lang.ada
 help / color / mirror / Atom feed
* on OO differnces between Ada95 and C++
@ 1996-02-20  0:00 Nasser Abbasi
  1996-02-20  0:00 ` Jerome Desquilbet
                   ` (5 more replies)
  0 siblings, 6 replies; 25+ messages in thread
From: Nasser Abbasi @ 1996-02-20  0:00 UTC (permalink / raw)




Hello,

I have a simple comment, but probably a long way of 
showing it :)

I have been playing around with the OO features in Ada95 and
comparing it with C++. I noticed this little difference, and I'd
like to see what you think of it.

Lets assume we have a base class called Account, and a class called
Saving_Account that uses Account as base. 

Lets also assume that we need to define a Money Type, defined in
the base class Account.     

In Ada95 this type is defined in the package Account.ads that also
includes the definition of the tagged record type Account (along with
operations that act on Account type).

In C++, this Money Type is typedef'ed inside the public part of the
class Account, and it becomes part of the public interface of the
base class.

so far so good.

Now, In Ada95, a client that wishes to use Saving_Account type (and
any operations on it) will "with" the Saving_Account Package.

Also, in C++, a client who wishes to use Saving_Account class will
include "saving_account.h"

There is some differences though.

In C++, the client to the saving_account class can also use the
Money_Type type (even though that is defined in
the base class Account) without having to include base class
"account.h", this is because Money_Type has become a public part of the
Saving_Account class when Saving_Account inherited Saving class.

In Ada95, the client of Saving_Account has no viability to Money_Type
type definition even though they with'ed Saving_account package, since
Money_Type is not a "inherited" by the Saving_Account package
from the Account package. This means that in Ada95, If one wants to
access things like type definitions that are not tagged, but 
used in defining components inside the tagged record, one must
"with" the client package and also packages that the client package
with'ed just to be able to have viability to those type definitions.

I understand the reason why in Ada95 one must do this,
a client "with"ing a package has only viability to the public part
of that package and not to any packages that that package may have
"with"ed. 

In this case, It seems the C++ way of having everything inside
the public base class becoming visible to clients of the derived class
that inherits the base class is more economical?. 
(everything means, data members, methods members and things like 
enum, typedefs, structs. etc..) In Ada95 only certain type 
(the tagged record type) and methods on this type can be seen 
by clients of the extended type.


One way to go around having in Ada95 to "with" both Account and
Saving_Account packages, is that in the Saving_Account package 
to introduce new type Money_Type made of the Account.Money_Type,
this way clients of Saving_Account can see Money_Type type by just
"with"ing Saving_Account package and not have to "with" account
package also just to use the Money_Type. 

But this means that type definitions has to be manually "propagated" from
one package to another so that clients of the last package in the chain
have only to "with" that last package. Any better way?

Just to put all this in example, (In case the above got to winded :) , 
I have an example to show I mean. I show the Ada code, and below it the 
C++ code. You see that in Ada, the main.adb had to "with" both Account 
and Saving_account, while in C++, the main had to only include 
"saving_account.h" .

thanks,
Nasser
 Learning_Ada95_OO_features...

------------------ account.ads ---------------------------
package Account is
 type Money_Type is new integer range 1..100; -- just as an example
 type Account_Type is tagged
   record
     Balance    : Money_Type;
     Account_Id : Integer; 
   end record;
end Account;

----------------- saving_account.ads ----------------
with Account;
Package Saving_Account is
 type Saving_Account_Type is new Account.Account_Type with
   record
     Interest : Account.Money_Type;
   end record;

   -- here we could also type this:
   --
   -- Type Money_Type is new Account.Money_Type
   --
   -- this way Money_Type can be made visible to clients
   -- of saving account without having to "with" Account package.
   -- we have propogated Money_Type to this package from the
   -- "with"ed package, so that clients to this package can see
   -- this type without having to "with" the Account package
   -- JUST to access Money_Type
   --
end Saving_Account;

--------------- main.adb ---------------------------

with Account;   -- only reason with'ed is to have visibility to
                -- money_type. in C++, the main can see the Money_Type
                -- through Saving_Account, and do not have to include
                -- the base class Account
with Saving_Account;
with ada.text_io; use ada.text_io;

procedure main is       
  package Money_IO is new Ada.Text_IO.Integer_IO(Account.Money_Type);
  The_Saving_Account : Saving_Account.Saving_Account_Type;
  The_Balance : Account.Money_Type; 
  begin
    Money_IO.Get(The_Balance);
    The_Saving_Account.Balance := The_Balance;
end main;

==============================

Now the C++ way:


--------------- account.h -------------------
class Account
{
  public:
  typedef int Money_Type;
  Money_Type Balance;
  int        Account_Id;
};

-------------- saving_account.h -----------------

#include "saving_account.h" 
class Saving_Account : public Account
{
  public:
  Money_Type Interest;
};

--------------- main.cpp -----------------

#include <iostream.h>
#include "saving_account.h"  // Notice: no need to include to base class
                             // Account in account.h
main()
{
Saving_Account             The_Saving_Account;
Saving_Account::Money_Type  The_Balance;  // notice, Money_Type accessed
                                          // through Saving_Account and
                                          // no need to include the
                                          // base class account

 cout << "Balance?";
 cin  >> The_Balance;
 The_Saving_Account.Balance = The_Balance;

return 1;

}







^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: on OO differnces between Ada95 and C++
@ 1996-02-21  0:00 Christian Jaensch, FRG
  0 siblings, 0 replies; 25+ messages in thread
From: Christian Jaensch, FRG @ 1996-02-21  0:00 UTC (permalink / raw)


Nasser Abbasi <nabbasi@QUALCOMM.COM> wrote:
> Hello,
>
> I have a simple comment, but probably a long way of
> showing it :)
>
> I have been playing around with the OO features in Ada95 and
> comparing it with C++. I noticed this little difference, and I'd
> like to see what you think of it.
[rest snipped...]

  Below, I slightly modified your example using the parent-child approach.
This gets rid of the aspect that seemed that have bothered you (explicit
with of package Account needed in main) in your approach.
BTW, whenever things are closely related or types are extended as in your
example, I preferably use the hierarchical package approach. It provides
-- if done well -- amongst other benefits a fairly transparent view of the
class hierarchy. Besides, the parent-child approach is needed if you make
all your to be extended types private ;-).

------------------ account.ads ---------------------------
package Account is

 type Money_Type is new integer range 1..100; -- just as an example

 type Account_Type is tagged
   record
     Balance    : Money_Type;
     Account_Id : Integer;
   end record;

end Account;

----------------- account-saving_account.ads ------------
Package Account.Saving_Account is

 type Saving_Account_Type is new Account_Type with
   record
     Interest : Money_Type;
   end record;

   -- ...
end Account.Saving_Account;

--------------- main.adb ---------------------------
with Account.Saving_Account; -- provides implicit with clause of parent
with Ada.Text_IO; use Ada.Text_IO;

procedure Main is

  package Money_IO is new Ada.Text_IO.Integer_IO(Account.Money_Type);

  The_Saving_Account : Account.Saving_Account.Saving_Account_Type;
  The_Balance        : Account.Money_Type;

begin

    Money_IO.Get(The_Balance);
    The_Saving_Account.Balance := The_Balance;

end Main;

I hope this helps,
--
    _/_/_/_/   _/_/_/_/  _/_/_/_/   Christian Jaensch   TEL: +49(0)881-64947
      _/      _/    _/        _/    Narbonner Ring 14   FAX: +49(0)881-637444
     _/      _/_/_/          _/     82362 Weilheim
    _/      _/    _/        _/      FRG
_/_/_/_/   _/_/_/_/    _/_/_/
C  O  N  S  U  L  T  I  N  G        em: chris@ifr.luftfahrt.uni-stuttgart.de
-----------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 25+ messages in thread
* Re: on OO differnces between Ada95 and C++
@ 1996-02-26  0:00 Simon Johnston
  0 siblings, 0 replies; 25+ messages in thread
From: Simon Johnston @ 1996-02-26  0:00 UTC (permalink / raw)


> ...YET ANOTHER RELIGIOUS WAR...

Lets hope not.

> Pete Becker wrote:
>
> > >#define private public // *** BERK! ***
> > >#include "...h"        // second definition for the same class
> > >#undef private
> >
> > This is not true. A program that attempts to do this violates the one
> > definition rule, so it is not a legal C++ program.
>
> Pete,
>
> What is this "one definition rule"? Could you point me to the
> appropriate C++ Draft Standard location?

  [snip]

> The whole program contains two definitions for the same classes Shape
> and Rectangle: for each, one definition with attributes private and
> another definition with everything public.

The whole program does, the point is that no one single compilation stream
must include more than one definition for a given entity.

> It seems that in C++, every encapsulation can be legally broken,
> essentially because of _independent_ compilation that allows
> redefinitions of the same class.
>
> Ada compilation model is different: it's _separate_ compilation.
> Impossible in Ada to have a redefinition in the same program: it's
> detected at compile-time. If something is checked in C++, it's deferred
> to link-time (for example a redefinition of the same class that will
> clash because the two constructors will have the same name).
>
>
> Read also "The design and evolution of C++" by Bjarne Stroustrup,
> chapter 17 "Namespaces", section 3 "Ideals for a solution": Ada has a
> more complete solution than C++.
>
> The basic property of OO languages is encapsulation. According to this
> criteria, C++ fails.
>
> Non mais enfin, quoi !
>
>
>   Jerome.
>
>

with StandardDisclaimer; use StandardDisclaimer;
package Sig is
--,-------------------------------------------------------------------------.
--|Simon K. Johnston - Development Engineer (C++/Ada95) |ICL Retail Systems |
--|-----------------------------------------------------|3/4 Willoughby Road|
--|Unix Mail: skj@acm.org                               |Bracknell          |
--|Telephone: +44 (0)1344 476320 Fax: +44 (0)1344 476302|Berkshire          |
--|Internal : 7261 6320   OP Mail: S.K.Johnston@BRA0801 |RG12 8TJ           |
--|WWW URL  : http://www.acm.org/~skj/                  |United Kingdom     |
--`-------------------------------------------------------------------------'
end Sig;




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

end of thread, other threads:[~1996-02-27  0:00 UTC | newest]

Thread overview: 25+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-02-20  0:00 on OO differnces between Ada95 and C++ Nasser Abbasi
1996-02-20  0:00 ` Jerome Desquilbet
1996-02-21  0:00   ` Robert Dewar
1996-02-22  0:00     ` Jerome Desquilbet
1996-02-24  0:00       ` Robert Dewar
1996-02-22  0:00   ` Pete Becker
1996-02-23  0:00     ` Jerome Desquilbet
1996-02-26  0:00     ` Darren C Davenport
1996-02-26  0:00       ` Pete Becker
1996-02-27  0:00         ` Nigel Perry
1996-02-20  0:00 ` Norman H. Cohen
1996-02-21  0:00   ` Mark A Biggar
1996-02-22  0:00     ` Norman H. Cohen
1996-02-27  0:00   ` Adam Morris
1996-02-20  0:00 ` Robert I. Eachus
1996-02-21  0:00 ` Jon S Anthony
1996-02-21  0:00 ` John English
1996-02-22  0:00   ` Nasser Abbasi
1996-02-26  0:00     ` John English
1996-02-27  0:00       ` Dale Stanbrough
1996-02-22  0:00   ` Gene Ouye
1996-02-26  0:00     ` John English
1996-02-21  0:00 ` Darren C Davenport
  -- strict thread matches above, loose matches on Subject: below --
1996-02-21  0:00 Christian Jaensch, FRG
1996-02-26  0:00 Simon Johnston

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