Method Example II


Question: Define object types point_t for points and rectangle_t for rectangles:
  • The area( ) method returns the area of the underlying rectangle object.
  • The inside(point_t) method returns 1 if the point argument is inside or on the boundary of the underlying rectangle object, and 0 otherwise.
SQL> CREATE TYPE  point_t  AS OBJECT (
  2    x  INT,
  3    y  INT );
  4  /

Type created.

SQL> CREATE TYPE  rectangle_t  AS OBJECT (
  2    pt1  point_t,
  3    pt2  point_t,
  4    MEMBER FUNCTION  inside(p point_t)  RETURN INT,
  5    MEMBER FUNCTION  area  RETURN INT);
  6  /

Type created.

SQL> CREATE TYPE BODY  rectangle_t  AS
  2    MEMBER FUNCTION  area  RETURN INT IS
  3    BEGIN
  4      RETURN (self.pt2.x - self.pt1.x) * (self.pt2.y - self.pt1.y);
  5    END;
  6
  7    MEMBER FUNCTION  inside(p in point_t)  RETURN INT IS
  8    BEGIN
  9      IF (p.x >= self.pt1.x) AND (p.x <= self.pt2.x) AND
 10         (p.y >= self.pt1.y) AND (p.y <= self.pt2.y) THEN
 11         RETURN  1;
 12      ELSE
 13         RETURN  0;
 14      END IF;
 15    END;
 16  END;
 17  /

Type body created.



      A penny for your thoughts, and half a penny for your land