Classes
Hybrix CLASS definitions have two main usages:
-
Defining compound data type - for example, defining your own
CIRCLEdata type is easier that managing three separateX,Y, andRADIUSvalues. We'll discuss that first. -
Object oriented programming - a classic software engineering model that associates functions with the data structures that they operate upon. This is covered in the following sections.
Compound data types
Suppose we want to manage three circles in our program. Maybe they are danger zones for a video game. A circle has a center (X,Y) and a radius R.
When we write functions that work with circles, normally we would need to pass these three values as parameters. For example:
MODULE MAIN
FUNC GET_SQUARED(VALUE: INT): INT
VAR RESULT: INT
VALUE * VALUE -> RESULT
RETURN RESULT
END FUNC
# THIS FUNC RETURNS TRUE IF THE TWO CIRCLES OVERLAP.
# THE FIRST CIRCLE HAS ITS CENTER AT (X1, Y1) WITH RADIUS R1.
# THE SECOND CIRCLE HAS ITS CENTER AT (X2, Y2) WITH RADIUS R2.
FUNC CHECK_CIRCLES(X1: INT, Y1: INT, R1, X2: INT, Y2: INT, R2:INT): BOOL
VAR SQUARED_DISTANCE: INT, TOUCHING: BOOL
# THE CIRCLES OVERLAP IF THE DISTANCE BETWEEN THEIR CENTERS
# IS SMALLER THAN THE SUM OF THEIR RADIUSES:
#
# SQRT( (X1-X2)*(X1-X2) + (Y1-Y2)*(Y1-Y2) ) > (R1+R2)*(R1+R2)
#
# TO AVOID A SQRT(), WE CAN SQUARE BOTH SIDES OF THIS EQUATION.
# CALCULATE THE SQUARED DISTANCE BETWEEN THE CENTERS
MAIN::GET_SQUARED(X1 - X2) + MAIN::GET_SQUARED(Y1 - Y2) -> SQUARED_DISTANCE
# IS IT LESS THAN THE SQUARED SUM OF THE RADIUSES?
IF SQUARED_DISTANCE < MAIN::GET_SQUARED(R1 + R2) THEN
TRUE -> TOUCHING
ELSE
FALSE -> TOUCHING
END IF
RETURN TOUCHING
END FUNC
FUNC START()
CONSOLE::INIT()
IF MAIN::CHECK_CIRCLES(0, 0, 5, 10, 20, 5) THEN
CONSOLE::PRINT("TOUCHING")
ELSE
CONSOLE::PRINT("NOT TOUCHING")
END IF
END FUNC
END MODULE
All these X1's and X2's quickly get confusing, however. Hybrix classes enable us to define new data type called CIRCLE that is easier to manage:
# DEFINE A CLASS CALLED "CIRCLE" CONTAINING THREE MEMBER VARIABLES
CLASS CIRCLE
VAR X: INT, Y: INT
VAR RADIUS: INT
END CLASS
MODULE MAIN
FUNC GET_SQUARED(VALUE: INT): INT
VAR RESULT: INT
VALUE * VALUE -> RESULT
RETURN RESULT
END FUNC
# THIS FUNC RETURNS TRUE IF THE TWO CIRCLES OVERLAP.
FUNC CHECK_CIRCLES(C1: CIRCLE, C2: CIRCLE): BOOL
VAR SQUARE_DISTANCE: INT, TOUCHING: BOOL
# CALCULATE THE SQUARED DISTANCE BETWEEN THE CENTERS
MAIN::GET_SQUARED(C1.X - C2.X) + MAIN::GET_SQUARED(C1.Y - C2.Y) -> SQUARE_DISTANCE
# IS IT LESS THAN THE SQUARED SUM OF THE RADIUSES?
IF SQUARE_DISTANCE < MAIN::GET_SQUARED(C1.RADIUS + C2.RADIUS) THEN
TRUE -> TOUCHING
ELSE
FALSE -> TOUCHING
END IF
RETURN TOUCHING
END FUNC
FUNC START()
VAR C1: CIRCLE, C2: CIRCLE
CONSOLE::INIT()
# CREATE A NEW INSTANCE OF CIRCLE AND STORE IT IN C1
NEW CIRCLE() -> C1
0 -> C1.X
0 -> C1.Y
5 -> C1.RADIUS
# CREATE A NEW INSTANCE OF CIRCLE AND STORE IT IN C1
NEW CIRCLE() -> C2
10 -> C2.X
20 -> C2.Y
5 -> C2.RADIUS
IF MAIN::CHECK_CIRCLES(C1, C2) THEN
CONSOLE::PRINT("TOUCHING")
ELSE
CONSOLE::PRINT("NOT TOUCHING")
END IF
END FUNC
END MODULE
Important points:
- Classes are custom data types defined using
CLASSfollowed by the class name. In our example, the class name isCIRCLE. - The value of a class is called an object or instance of the class.
- Instances are created using
NEW, for exampleNEW CIRCLE(). (The Hybrix garbage collector will free the memory automatically.) - The
NEWoperator will initialize the memory block to all zeros. For example,NEW CIRCLE()will start out with 0 in.X,.Y, and.RADIUSmembers. - Class variables are called pointers because they store the memory address of the instance. In the example, it is possible for
C1andC2to point to the same instance. For example, if we didC1 -> C2, then subsequent changes toC1would affectC2as well. - If a pointer does not point to anything, then its value is
NULL. We could writeNULL -> C1, for example. - Class members are accessed using the
.operator. For example,C1.Xrefers to theXvariable ofC1. - If the
.operator is applied to a variable whose value isNULL, your program will fail with code0.
You can also use CLASS to make nesting data structures such as a linked list or tree.
# SINGLE-LINKED LIST NODE
CLASS LINK_NODE
VAR VALUE: INT
VAR NEXT: LINK_NODE
END CLASS
# BINARY TREE NODE
CLASS TREE_NODE
VAR VALUE: INT
VAR LEFT: TREE_NODE
VAR RIGHT: TREE_NODE
END CLASS