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.
01SQL> CREATE TYPE  point_t  AS OBJECT (
02  2    x  INT,
03  3    y  INT );
04  4  /
05 
06Type created.
07 
08SQL> CREATE TYPE  rectangle_t  AS OBJECT (
09  2    pt1  point_t,
10  3    pt2  point_t,
11  4    MEMBER FUNCTION  inside(p point_t)  RETURN INT,
12  5    MEMBER FUNCTION  area  RETURN INT);
13  6  /
14 
15Type created.
16 
17SQL> CREATE TYPE BODY  rectangle_t  AS
18  2    MEMBER FUNCTION  area  RETURN INT IS
19  3    BEGIN
20  4      RETURN (self.pt2.x - self.pt1.x) * (self.pt2.y - self.pt1.y);
21  5    END;
22  6
23  7    MEMBER FUNCTION  inside(p in point_t)  RETURN INT IS
24  8    BEGIN
25  9      IF (p.x >= self.pt1.x) AND (p.x <= self.pt2.x) AND
26 10         (p.y >= self.pt1.y) AND (p.y <= self.pt2.y) THEN
27 11         RETURN  1;
28 12      ELSE
29 13         RETURN  0;
30 14      END IF;
31 15    END;
32 16  END;
33 17  /
34 
35Type body created.



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