Objects First


Introduction

Why do we write computer programs?

Asking this question produces many answers, for example:

In each of these applications, a programmer creates some model of a world (real or unreal) in a computer program. This program manipulates objects in this model of the world or domain in which it operates. The weather forecasting program models winds, ocean currents, clouds, etc. The bank's program models the old paper ledgers which banks used to keep their customers' accounts before computers were invented. The factory control program has models for the robots which assemble its products, the conveyor belts which move partially assembled products between processing stations and the products themselves.

Engineering and Hacking

It is easy to write a simple computer program which will perform long and complex calculations in a fraction of the time it would take us to make the same computation by hand. However, some painful early experience has shown us that there is a real difference between a well-engineered program and one that simply produces a correct result. Of course, for a small program, this difference is minimal and, since our well-engineered program make take a little longer to write, we may well decide to sacrifice elegance and maintainability for speed. Thus while it may be acceptable to cobble together a quick solution to a small problem (this process is usually referred to as hacking), applying the same approach to a large problem is known to lead to disasters.
The history of computer programming is full of tales of programs that
  • terminate unpredictably (crash),
  • do not produce the correct answers (have bugs in abundance),
  • are expensive to maintain, and
  • contain hidden errors (timebombs) that will manifest themselves at some time in the future
and their (sometimes tragic) consequences.
So that we can write programs which are reliable (produce the correct answers) and robust (don't crash), a discipline known as software engineering has formed. Professional programmers follow a set of rules (or a methodology) which assists them to engineer reliable and robust programs.

Object orientation

The most recent advance in software engineering is the object orientation(OO) design strategy. Using this strategy, a software engineer designs a set of classes which describe the behaviour of objects in the world in which his or her program operates. Object oriented design allows the programmer to build software structures that are robust and reliable as well as being easy to maintain.

Classes

The class is the key concept in OO design. Classes are descriptions of objects from the real (or virtual) world that we are modelling with our program. Objects are instances of classes. Thus a class describes the behaviour of a group of actual objects in our model.

Black Boxes

Classes are black boxes: a programmer using a class does not care how the class performs its operations!

Here we have a model of a calculator.

We feed it a request - evaluate 12 / 3 ....

and it produces an answer for us.

We don't really care how it produces the answer for us!

The technology used can be quite ancient ..

or there may not be much technology at all!

But we are usually concerned about the accuracy of the result!

No matter how fast the system, it's of little use if it produces wrong answers.

Or in the case of the notorious Intel divide bug, almost right answers.

To their users (programmers using classes to write programs), classes are abstract - they have certain behaviours which are clearly and unambiguously defined. In the first stage of a software system's development, users don't care how any class is implemented. Later on, when speed and resource usage may become issues, the designers of the class can substitute more efficient models for the original ones.

Class components

Classes have two components: For example, let's consider a "world" of geometric objects, points, lines, rectangles, etc. One class of objects in this world might be rectangles. Rectangles in this world have many properties, let's start by considering only two: width and height. These are two of the attributes of a rectangle class. The rectangle class will have methods - which we will name for convenience -
NameDescription
ConsRectanglecreate (construct) a rectangle
DestroyRectangledestroy a rectangle
Widthobtain the width
Heightobtain the height
SetWidthchange the rectangle's width
SetHeightchange the rectangle's height
In order to make this class useful, we will add some methods that derive properties of objects in the class:
Perimetercalculate the length of the perimeter
Areacalculate the area

We can now build a formal software specification of the rectangle class.
First, we give the class a name:

typedef struct rectangle *Rectangle;

Later, we shall dissect this statement in detail: for the moment, we'll simply use the pattern:

typedef struct xx *

to precede the name of a new class.

From now on, we will use the courier font for parts of actual programs. Anything written in this font would be placed into a file which is processed by the compiler into a program.

Programs are prepared by placing the program statements (which appear in courier font in these notes) in text files which are processed by a compiler into an executable program. You can prepare a program file by using any convenient text editor on your system. See also function which takes two parameters, height and width which are both double precision real numbers (type double) and returns a Rectangle. Functions in programming languages are analogous to mathematical functions, so just as we would write:

y = f(x)
we write:
r = ConsRectangle( 20.0, 40.0 );
This statement can be read:
Invoke the function that creates (or constructs) a Rectangle with height 20 and width 40 and assign the newly constructed object to a variable named r.
Another convention

Object creation functions are called constructors: thus we choose to name our creation function, ConsRectangle. We will use this naming convention for all constructors, so that a constructor for an object of a class X will be called ConsX.

Once we have constructed an object as an instance of a class, we can invoke other methods in a similar fashion: first we must add definitions for the them to the definition for a constructor:
  • Rectangle ConsRectangle( double height, double width );
  • void DestroyRectangle( Rectangle r );
  • double Height( Rectangle r );
  • void SetHeight( Rectangle r, double height );
  • double Width( Rectangle r );
  • void SetWidth( Rectangle r, double width );
  • double Perimeter( Rectangle r );
  • double Area( Rectangle r );
Now we can invoke them: for example:
h = Height( r );
invokes a method which obtains the height of the rectangle and assigns the value thus obtained to a variable h.
SetWidth( r, 30.0 );
changes the height of Rectangle, r, to 30.

Class definition

The name of the class and the list of its methods constitute the definition or specification of the class. Our first step in writing the software model for rectangles is to place them in a file called
rectangle.h.

Where are the attributes?

Note that the formal specification does not include any mention of the attributes of the Rectangle class. This is in line with the abstract nature of the class. The attributes are hidden in the implementation of the class - which we'll see later.

Key terms

hacking
producing a computer program rapidly, without thought and without concern for others who might have to modify the program later - an unprofessional approach
crash
a failure of a computer program to terminate as expected; failure of a program to run to completion
bug
an error in computer software or hardware which produces an error in its output or causes it to crash
software engineering
an engineering discipline applied to the writing of computer programs so that they are reliable (produce the right answers) and robust (don't crash)
methodology
a set of rules or procedures - in this case for designing software systems
object orientation
a methodology for designing computer programs in which software models of objects in some real or virtual world are constructed. Please note that the past participle from object orientation is object oriented, not object orientated as many people seem to want to say!
class
a software model of a generic class of objects which a program is manipulating
abstract
Only the behaviour of a class is specified: the means by which this behaviour is realised is not specified - it's of no concern to the user of the class who's only interested in the behaviour and its correctness
object
an instance of a class - corresponds to an actual object in some real or virtual world that the program is modeling; individual objects may be distinguished by having different values of their attributes
attribute
a characteristic of an object of a class that can be assigned some value (numeric or symbolic)
method
an operation which can be applied to an object to either
  • find out about its state - the values of its attributes or
  • alter its state
software specification
a program file formally defining a class - it contains the class' name and specifications of the class methods
constructor
special method of a class which creates (constructs) new objects of that class
function
component of a computer program which takes a set of values (its arguments or parameters) and produces a result. The methods of a class are C language functions. Simple functions in programming languages are analogous to mathematical functions. However, in general, they are much more powerful than mathematical functions; they can return multiple values, change the state of their arguments, etc. See Functions.
parameters
values which are passed to a function when it is invoked: these values are used by the function to perform some operation

Continue on to Introduction to Input and Output Back to the Table of Contents
© John Morris, 1999