Instructor's Manual for
M.B. Feldman and E.B. Koffman, Ada 95 Problem Solving and Program Design, 3rd edition. Copyright 1999, Addison-Wesley Publishing Company. All Rights Reserved
Questions and comments to mfeldman@seas.gwu.edu
last revised March 1999
The student will
object-oriented programming (OOP) |
encapsulation |
genericity |
inheritance |
polymorphism |
dynamic dispatching |
object state |
object behavior |
object-based programming |
tagged type |
type extension |
type hierarchy |
up-conversion |
primitive operation |
derived type |
overriding |
general access type |
pool-specific access type |
ALIASED variables |
'Access attribute |
class-wide type |
heterogeneous array |
dynamic dispatching |
heterogeneous linked list |
This chapter is intended to whet the student's appetite for further study of inheritance-oriented programming. Space in this book did not permit anything like a full discussion of the topic. For example, we have omitted all discussion of abstract tagged types and subprograms.
Object-oriented programming is a paradigm that relies on several aspects of programming languages, of which inheritance and dynamic polymorphism are just two. These are available in Ada 95. Ada 83 already provided support for encapsulation--packages and private types--and static polymorphism--subprogram overloading and generic templates. It is sometimes asserted that programming without inheritance is simply not OOP, but the growing popularity of templates in C++ suggests that generic programming is seen as legitimate OOP as well.
As is pointed out in this section, the most fundamental aspect of OOP is that an object has state and behavior. This book does not insist on those specific terms, but certainly emphasizes from the start that each object (variable) has a type, and that its type characterizes its state (value) and behavior (available operations).
In this section we introduce extendable (tagged) records as a generalization of variant records; we think this is historically accurate. Students with experience in other object-oriented languages may want to know why Ada 95 supports inheritance via types and nor, say, via a generalization of packages to emulate "classes." You can explain that type derivation and weak inheritance already existed within the Ada 83 type system. Further, Ada distinguishes between encapsulation (packages) and types. A "class" construct that blurred the distinction would have created a very different language instead of a smooth extension to an existing one. Ada 95's OO syntax does not mimic that of other languages, but Ada 95 nevertheless provides the functionality to do OOP within an Ada-like framework.
Devote some time to explaining just what a primitive operation is, why we do not want a constructor to be primitive, and therefore why we placed the constructors in inner packages. Child packages are also acceptable for his purpose; we chose the inner-package structure for human-factors reasons, namely so that all the operations are located in one place.
We chose to introduce general access types in this chapter because they are--at the level of this book--more relevant to the problem of creating heterogeneous arrays and lists than to the dynamic structures of Chapter 14. Students may ask why variables must be marked ALIASED in order to point to them with general access values. Aside from the obvious readability advantage, if arbitrary variables could be pointed to in this manner, it would be far more difficult for the compiler to make sure that variables could not be referenced beyond their scope.
Explain why we can not create an array of classwide values, but must instead create an array ofpointers to these values.
Explain the difference between the five Put invocations in Program 16.4 and the five invocations of Put in the FOR loop of Program 16.9. In the earlier case, each Put was determined by ordinary overload resolution at compilation time; here the resolution (dispatching) is done at execution time. These two superficially similar programs have very different behavior!
Here we tie together the generic list package of Section 15.5 with the tagged types of this chapter to create a heterogeneous linked list with a very few statements. The combination of generics and inheritance makes for an interesting discussion.