6.5 Summary.

We have now covered nearly all of the Ada95 core language; in particular we have now covered the Ada95 object oriented programming constructs and facilities. In most cases it is relatively easy to rewrite C++ classes as Ada95 tagged types, if multiple inheritance, base class access, or friends are extensively used, then some class libraries may require some redesign. In general though the object oriented features of Ada95 are well integrated into a safer, more complete language than C++ and the effort required to rework some of the more archaic C++ in use today is worth the investment.

Ada95 was not reengineered to be a pure object oriented language such as Smalltalk, it was meant to provide a set of additional programming features to programmers who were used to a safe, productive language and did not want to have to fix code broken by a new language version. This is a similar goal to that of the original designers of C++ when moving from C.

How many times has a new C++ compiler arrived which has introduced new type rules, new types, new headers and a lot of backwards compatibility problems? The designers of Ada95 have tried to minimise the impact of the language changes, though of course if you have a variable called aliased or tagged then you do have a minor problem.

The following is a brief summary of tagged type features:

package Animal is 

   -- abstract tagged type.
   type Instance is abstract tagged private;

   -- class wide type.
   subtype Any_Animal is Instance'Class;

   -- virtual subprogram.
   procedure Eat
     (Animal : in Any_Animal;
      Amount : in Integer);

   -- pure virtual subprogram.
   procedure Make_A_Sound
     (Animal : in Instance)
      is abstract;

private
   type Instance is abstract tagged 
      record
         -- data member.
         Food_Reserves : Integer := 100;
      end record;

   -- protected subprogram.
   function Is_Hungry
     (Animal : in Instance)
      return Boolean;

end Animal;


package Animal.Dog is

   -- derived type.
   type Instance is new Animal.Instance with private;

   -- override pure virtual subprogram.
   procedure Make_A_Sound
     (Dog : in Instance);

   -- additional subprogram.
   procedure Chase_Stick
     (Dog : in Instance);

private
   type Instance is abstract tagged 
      record
         -- additional data member.
         Has_Fleas : Boolean := False;
      end record;

end Animal.Dog;

Ada_Store We have also introduced some new features and packages into our example. We introduced in 6.2 the Ada_Store.Trading.Item package, which also referenced another package, Ada_Store.Trading.Department. The packages below Ada_Store.Trading represent entities required as part of the stores trading operations and as well as items and departments tenders (payment types) and customer sales have packages. All the types represented by these packages are marked tagged in case we need to derive from them at later stages. We can also introduce one of our most important tagged types, the device type, an abstract type from which we will derive individual peripheral handling types.

Example Ada_Store.PoST.Device.ads

The type itself is an abstract tagged type, and so when derived to provide a concreete type (as in the scanner case below) the abstract subprograms Open, Flush and Accept_Input must be implemented.

The example below also shows how we can overload a function specified in a parent type, in this case Current_Status, with an additional meaning.

Example - Ada_Store.PoST.Device.Scanner.ads

Quickly here is the body to our base device package.

Example - Ada_Store.PoST.Device.adb


Previous PageContents PageNext Page

Copyright © 1996 Simon Johnston &
Addison Wesley Longman