[prev] [index] [next]

Example: Point ADT (cont)

PointADT.c gives implementation

#include "PointADT.h"

struct PointRep { float x; float y; };

Point new(float xpos, float ypos) {
   Point p = (Point)malloc(sizeof (struct PointRep));
   assert(p != NULL);
   p->x = xpos;  p->y = ypos;
   return p;
}
float distance(Point a, Point b) {
   float dx = a->x - b->x;
   float dy = a->y - b->y;
   return sqrt(dx*dx + dy*dy);
}
void move(Point a, float dx, float dy) {
   a->x += dx;
   a->y += dy;
}
void delete(Point a) {
   free(a);
}