Week 8 - Chapter 11 - Abstract Data Types

Theory

Abstract Data Types

Encapsulation (p. 469) is one of the key concepts of Object-Oriented Programming. It basically means you can take a data structure and the functions that operate on it and wrap it up into a single module. Most programming languages call this a class. (Ada doesn't.) As you may recall from week 1 notes, this is the hallmark of "The Age of Objects".

Data Abstraction (p. 470) descends from encapsulation. After we've made an ADT, we can simply focus on the operations that will be performed on it and we don't concern ourselves with what's going on "under the hood". If we do give any thought to the object's properties, we simply consider it's "state" in abstract terms; the programmer need never know all the details inside the ADT.

Type (also p. 470) helps to enforce the idea that "this type goes with these functions". The strong-typing in a language is one mechanism for enforcing the association of particualr types with particular functions.

The Nature of an ADT

Look at the section entitled "The Nature of an ADT" on page 472. This section covers a few of the things that were covered in the week 2 notes. The high points are:

Object Concepts

Although object implementations differ from language to language, there are some things which all objects can/should have in common. See pages 473 - 474 for an intro to some of these concepts. I have restated the lists in your book here and added some additional concepts that the book did not mention.

Pure Object Orientedness?

Purists have their own definition for what qualifies a language as being a "pure" object oriented language:

In practice, very few OO languages are "Pure" by this definition. Even the much balyhooed Java doesn't meet the criteria (has primitive types). Neither does Ada, for a number of reasons. To the best of my knowledge, Eiffel and Smalltalk are the only "Pure" OO languages, but there may be others.

Probably the reason why there are so few "Pure" OO languages is because such strictness makes them a little awkward, and programmers like shortcuts (cf. laziness...).

Object-Oriented vs. Object-Based

There is an important difference that needs to be explained at this point:

An Object-Oriented language allows you to both use and create objects. Examples include C++, Java, Python, and (you guessed it) Ada.

An Object-Based language only allows you to use objects, you cannot create your own. Visual Basic and JavaScript are examples.

Approaches to ADTs / OO programing in Various Languages

Bear in mind that OO programming can be accomplished in any language that allows you to create a struct or RECORD like construct. In class, I'll go through several examples of how a file object could be used.

If you're interested, take a look at the GTK library, which is an object-oriented toolkit written in C.

Ada Stuff for Chapter 11

Declaring an ADT in Ada looks a lot more like C than C++. An ADT is declared by making a declaration in a specification (.ads) file that starts with:

	PACKAGE Something IS
		TYPE ADT_Name IS PRIVATE;

Then in the definition (.adb) file you define what the type is. The type can be anything: an enumeration, an array, or a record. The client program that uses the ADT has no idea what he's acutally using. (This is data abstraction.)

The chapter is rife with examples. Start with the listings for Float and Integer on pgs. 470-472. Then, look at the specification for the Calendar package on p. 475-6. (Note the operator overloading in both, more on this in just a minute.)

Constructors (mentioned in the middle of p. 477) Note that there is no special syntax for making constructors, you just make a function that returns an ADT. The Time_Of function in the Ada.Calendar listing on p. 475 is an example. If you come from a C++ / Java / Python background, this might seem a little weird to you. Don't worry, we'll all get through this together.

Private Types - Look at the Simple_Dates package declared on p. 376-7. Got it? Good. Now, look at the vastly improved Dates package on p. 482-3. Declaring the record as PRIVATE makes it such that the client program cannot directly manipulate the fields of the record; it is an access modifier. Syntax Summary p. 483. Also, look at the "Advantages of Private Types" white box on p. 503.

User-Definied Exceptions (example in listing on p. 482) Declared with Identifier : EXCEPTION, raised with RAISE Identifier. Syntax summary p. 484. Note the example of how it's raised on p. 485.

Private Functions / Procedures (white box, top of p. 490) To make a private function / procedure in Ada (i.e. one that will be inaccessable by client programs), you simply define it in the implementation (.adb) file, but don't declare it in the specification (.ads) file. (IOW, you don't put 'private' in front of them like you would in, say, C++). Pretty simple.

Operator Overloading - Look at the specification for the Currency package on p. 494 to see how operator overloading is declared. You can then see how the implementation is done on p. 497. Operator overloading is pretty clean in Ada. I like the fact that you don't have to use a keyword to specify that you're overloading a operator, just surround it with quotes. Couple of things to note: You can't change the precedence of any of the existing operators, and you cannot create new operators out of whole cloth. Syntax Summary p. 502.

The USE TYPE discussion on p. 501 describes how you can use this special-case of a USE statement to specify which ADT you want to use, so as to retain the benefit of user-defined operators in a program that WITHs a whole bunch of other files.

Chapter Summary p. 516.

Assignment 7

Write a package which implements a quadratic equation. As we all remember from our math classes of yesteryear, a quadratic equation comes in the following form:

 [-b +/- sqrt(b^2 - 4ac)] / 2a 

So, here's what I want to see:

File Names

As some of you have already figured out, this assignment will require more than one file. More to the point, it will require three files. I would like you all to use the following file names:

This assignment is due on October 31st, 2001 the night of the second exam.