Objects First


9.2 Class Design - Point Example

In this example, we examine a model for points in a 2-dimensional space. The class is called Point and contains methods to construct points from either cartesian (x,y) or polar (r,theta) coordinates. There's also a method to make a copy of a point, so that you can view this class as having three constructors!

The remaining methods are straightforward:

NameDescription
XCoord( p )
YCoord( p )
RPolar( p )
TPolar( p )
Projectors for the:
x-coordinate, y-coordinate, distance from origin, angle from origin
Dist( p1, p2 ) Distance between p1 and p2
Displace( p1, delta ) Displace p1 by delta
Direction( p1, p2 ) Direction from p1 to p2
SetPoint( p, x, y ) Set p to (x,y)
SetPoint2( p, p1 ) Set p to be the same as p1

The examples themselves:

Specification Point.h
Implementation Point.c

The observant among you will have noted some required features, such as pre-conditions, missing!

This has been a consequence of rushing this example to the market-place and is not an excuse for omitting them yourselves!


Back to the Table of Contents
© John Morris, 1997