Section 7.5 - Standard Object-Oriented Format

Ada has types, packages, and lots of other things; how do you use them all together? What I suggest is that, barring other information, you use a standard format for defining object-oriented types in Ada.

Here's a "standard format" that I use for an OO Ada type definition, which you can vary to meet your needs (capitalized italics are what you supply):

  with PACKAGE_NAME_OF_PARENT;
  use  PACKAGE_NAME_OF_PARENT;
  package PACKAGE_NAME is
    type MY_TYPE is tagged private; -- or, new Parent_Type with private
    type MY_TYPE_Access is access all MY_TYPE'Access;
        -- We'll talk about this in a later lesson; by adding this declaration
        -- here you'll make certain operations easier to do later.
    -- Dispatching operations go here.
  private
    -- Define the details of MY_TYPE here.
  end PACKAGE_NAME;

For the package name, I generally use the plural of the type name. Here's an example:

  with Creatures;
  use  Creatures;

  package Players is
    type Player is new Creature with private; -- Player is a type of Creature
    type Player_Access is access all Player'Access;
    -- Dispatching operations go here.
    procedure Look(P : in Player);
  private
    type Player is new Creature with
     record
       Logged_In : Boolean;
     end record;
  end Players;

Ada permits multiple OO types to be defined in a package, but that doesn't mean you must do so. As a general rule, put different type definitions in different packages unless the two types are strongly interrelated.


Quiz:

Given the example above, what is the name of the new tagged type defined above?

  1. Creatures
  2. Players
  3. Player

You may also:

PREVIOUS Go back to the previous section

NEXT     Skip to the next section

OUTLINE  Go up to lesson 7 outline

David A. Wheeler (dwheeler@dwheeler.com)

The master copy of this file is at "http://www.adahome.com/Tutorials/Lovelace/s7s5.htm".