6.2 Tagged types.

In a number of places in the text so far we have made mention of tagged types, a feature added in Ada95 to provide for object oriented programming. Tagged types are an extension to the existing concept of a record, and provide the run-time information required to make dispatching calls to primitive operations (virtual functions) therefore providing polymorphism. They also provide a method for extending a type when deriving it and so complete our C++ class feature list above.

Example - Animal.ads

You have now seen a tagged type declaration, please note the following additional features:

  1. The use of the keyword tagged when declaring the type Instance.
  2. The use of the keyword abstract when declaring both the procedure Make_A_Sound and the type Instance.
  3. The use of the attribute 'Class on the formal parameter to the procedure Make_A_Sound.
  4. The absence of an enclosing construct such as the class in C++ which delimits its members. The tagged type does not act in this way.

    Example - Animal.adb

The above is a simple form of the package body for the package Animal.

Note: in the above package the main type is called Instance, this is a common convention which does have some advantages. Mainly it uses the package/type relationship and the namespace management of Ada to provide meaningful names. Other conventions are to use plurals for package names and use the singular for the type, for instance:

package Animals is
   type Animal is tagged ..

   ..

   An_Animal : Animal;
   A_Dog : Animals.Dogs.Dog;

Previous PageContents PageNext Page

Copyright © 1996 Simon Johnston &
Addison Wesley Longman