|
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.
- 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
© John Morris, 1999
|
|