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


Chapter 16 Programming with Objects: Tagged Record Types

last revised March 1999

Chapter Objectives

The student will

  1. learn about tagged types and inheritance
  2. learn about general access types
  3. learn how to create heterogeneous arrays and linked lists

New Terms

Section 16.1

object-oriented programming (OOP)

encapsulation

genericity

inheritance

polymorphism

dynamic dispatching

object state

object behavior

object-based programming

Section 16.2

tagged type

type extension

type hierarchy

up-conversion

primitive operation

derived type

overriding

Section 16.3

general access type

pool-specific access type

ALIASED variables

'Access attribute

Section 16.4

class-wide type

heterogeneous array

Section 16.5

dynamic dispatching

Section 16.6

heterogeneous linked list

General Suggestions

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.

Section 16.1

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).

Section 16.2

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.

Section 16.3

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.

Section 16.4

Explain why we can not create an array of classwide values, but must instead create an array ofpointers to these values.

Section 16.5

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!

Section 16.6

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.