Concepts of Programming Languages

(Sean Pound) #1
12.9 Support for Object-Oriented Programming in Ada 95 559

programming in Ada 95 is complicated and that this section includes only a
brief and incomplete description of it.

12.9.1 General Characteristics


Ada 95 classes are a new category of types called tagged types, which can be
either records or private types. They are defined in packages, which allows
them to be separately compiled. Tagged types are so named because each object
of a tagged type implicitly includes a system-maintained tag that indicates its
type. The subprograms that define the operations on a tagged type appear
in the same declaration list as the type declaration. Consider the following
example:

with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Person_Pkg is
type Person is tagged private;
procedure Display(P : in Person);
private
type Person is tagged
record
Name : Unbounded_String;
Address : Unbounded_String;
Age : Integer;
end record;
end Person_Pkg;

This package defines the type Person, which is useful by itself and can also
serve as the parent class of derived classes.
Unlike C++, there is no implicit calling of constructor or destructor sub-
programs in Ada 95. These subprograms can be written, but they must be
explicitly called by the programmer.

12.9.2 Inheritance


Ada 83 supports only a narrow form of inheritance with its derived types and
subtypes. In both of these, a new type can be defined on the basis of an exist-
ing type. The only modification allowed is to restrict the range of values of the
new type. This is not the kind of full inheritance required for object-oriented
programming, which is supported by Ada 95.
Derived types in Ada 95 are based on tagged types. New entities are added
to the inherited entities by placing them in a record definition. Consider the
following example:

with Person_Pkg; use Person_Pkg;
package Student_Pkg is
type Student is new Person with
Free download pdf