Concepts of Programming Languages

(Sean Pound) #1

560 Chapter 12 Support for Object-Oriented Programming


record
Grade_Point_Average : Float;
Grade_Level : Integer;
end record;
procedure Display(St : in Student);
end Student_Pkg;

In this example, the derived type Student is defined to have the entities of its
parent class, Person, along with the new entities Grade_Point_Average
and Grade_Level. It also redefines the procedure Display. This new class
is defined in a separate package to allow it to be changed without requiring
recompilation of the package containing the definition of the parent type.
This inheritance mechanism does not allow one to prevent entities of the
parent class from being included in the derived class. Consequently, derived
classes can only extend parent classes and are therefore subtypes. However,
child library packages, which are discussed briefly below, can be used to define
subclasses that are not subtypes.
Suppose we have the following definitions:

P1 : Person;
S1 : Student;
Fred : Person := (To_Unbounded_String("Fred"),
To_Unbounded_String("321 Mulberry
Lane"), 35);
Freddie : Student :=
(To_Unbounded_String("Freddie"),
To_Unbounded_String("725 Main St."),
20, 3.25, 3);

Because Student is a subtype of Person, the assignment

P1 := Freddie;

should be legal, and it is. The Grade_Point_Average and Grade_Level
entities of Freddie are simply ignored in the required coercion. This is
another example of object slicing.
The obvious question now is whether an assignment in the opposite direc-
tion is legal; that is, can we assign a Person to a Student? In Ada 95, this
action is legal in a form that includes the entities in the subclass. In our example,
the following is legal:

S1 := (Fred, 3.05, 2);

Ada 95 does not provide multiple inheritance. Although generic classes
and multiple inheritance are only distantly related concepts, there is a way to
achieve an effect similar to multiple inheritance using generics. However, it is
not as elegant as the C++ approach, and it is not discussed here.
Free download pdf