object_20orientated_20programming
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
object_20orientated_20programming [2018/03/31 13:19] – external edit 127.0.0.1 | object_20orientated_20programming [2024/01/05 00:22] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 2: | Line 2: | ||
//by Richard Russell, April 2011//\\ \\ BBC BASIC isn't an Object Oriented (OO) language, but the **CLASSLIB** library (see [[/ | //by Richard Russell, April 2011//\\ \\ BBC BASIC isn't an Object Oriented (OO) language, but the **CLASSLIB** library (see [[/ | ||
+ | <code bb4w> | ||
DIM Ellipse{a, b, _set_size, _get_area} | DIM Ellipse{a, b, _set_size, _get_area} | ||
+ | </ | ||
Here **a** and **b** are the members (representing the lengths of the semi-minor and semi-major axes respectively) and **_set_size** and **_get_area** are the methods (used to set the size of the ellipse and return its area respectively). You may wish to choose a naming convention to make it clear which are the **members** and which are the **methods**, | Here **a** and **b** are the members (representing the lengths of the semi-minor and semi-major axes respectively) and **_set_size** and **_get_area** are the methods (used to set the size of the ellipse and return its area respectively). You may wish to choose a naming convention to make it clear which are the **members** and which are the **methods**, | ||
+ | <code bb4w> | ||
DEF Ellipse._set_size (a,b) Ellipse.a = a : Ellipse.b = b : ENDPROC | DEF Ellipse._set_size (a,b) Ellipse.a = a : Ellipse.b = b : ENDPROC | ||
DEF Ellipse._get_area = PI * Ellipse.a * Ellipse.b | DEF Ellipse._get_area = PI * Ellipse.a * Ellipse.b | ||
+ | </ | ||
We could also have specified a **constructor** (a method which is called automatically when the object is created) and/or a **destructor** (called when the object is discarded) but for simplicity this example doesn' | We could also have specified a **constructor** (a method which is called automatically when the object is created) and/or a **destructor** (called when the object is discarded) but for simplicity this example doesn' | ||
+ | <code bb4w> | ||
PROC_class(Ellipse{}) | PROC_class(Ellipse{}) | ||
+ | </ | ||
Now the class **Ellipse** is fully defined. Before we can use it, we must // | Now the class **Ellipse** is fully defined. Before we can use it, we must // | ||
+ | <code bb4w> | ||
PROC_new(myellipse{}, | PROC_new(myellipse{}, | ||
+ | </ | ||
Having created the object **myellipse** we can perform operations on it:\\ | Having created the object **myellipse** we can perform operations on it:\\ | ||
+ | <code bb4w> | ||
PROC(myellipse._set_size)(10.0, | PROC(myellipse._set_size)(10.0, | ||
PRINT "Area of ellipse is "; FN(myellipse._get_area) | PRINT "Area of ellipse is "; FN(myellipse._get_area) | ||
+ | </ | ||
Finally when we've finished with the object we release the memory it occupied: | Finally when we've finished with the object we release the memory it occupied: | ||
+ | <code bb4w> | ||
PROC_discard(myellipse{}) | PROC_discard(myellipse{}) | ||
+ | </ | ||
Another feature provided by most Object Orientated languages is **inheritance**. This allows a subclass to inherit the members and methods from a parent class. Suppose we now want to create a class for a circle. We could start from scratch, but a circle is a special case of an ellipse so instead we can create a **Circle** class which inherits from the Ellipse class: | Another feature provided by most Object Orientated languages is **inheritance**. This allows a subclass to inherit the members and methods from a parent class. Suppose we now want to create a class for a circle. We could start from scratch, but a circle is a special case of an ellipse so instead we can create a **Circle** class which inherits from the Ellipse class: | ||
+ | <code bb4w> | ||
DIM Circle{_get_circumference} | DIM Circle{_get_circumference} | ||
PROC_inherit(Circle{}, | PROC_inherit(Circle{}, | ||
PROC_class(Circle{}) | PROC_class(Circle{}) | ||
+ | </ | ||
The class inherits the **_get_area** method from its parent, but the Circle class has an extra **_get_circumference** method. For convenience we can also override the inherited **_set_size** method with a new version which takes only one parameter (the radius): | The class inherits the **_get_area** method from its parent, but the Circle class has an extra **_get_circumference** method. For convenience we can also override the inherited **_set_size** method with a new version which takes only one parameter (the radius): | ||
+ | <code bb4w> | ||
DEF Circle._get_circumference = 2 * PI * Circle.a | DEF Circle._get_circumference = 2 * PI * Circle.a | ||
DEF Circle._set_size (r) Circle.a = r : Circle.b = r : ENDPROC | DEF Circle._set_size (r) Circle.a = r : Circle.b = r : ENDPROC | ||
+ | </ | ||
Now we can use the derived Circle class: | Now we can use the derived Circle class: | ||
+ | <code bb4w> | ||
PROC_new(mycircle{}, | PROC_new(mycircle{}, | ||
PROC(mycircle._set_size)(10.0) | PROC(mycircle._set_size)(10.0) | ||
Line 28: | Line 45: | ||
PRINT " | PRINT " | ||
PROC_discard(mycircle{}) | PROC_discard(mycircle{}) | ||
+ | </ | ||
Here **mycircle._set_size** is the overridden method, **mycircle._get_area** is the method in the parent **Ellipse** class, and **mycircle._get_circumference** is the additional method in the **Circle** subclass.\\ \\ It is possible to create a subclass of **Circle**, thus creating a hierarchy of classes. Suppose we want to represent a circle in 2D space, which will therefore require additional data representing its position and an additional method to set the position: | Here **mycircle._set_size** is the overridden method, **mycircle._get_area** is the method in the parent **Ellipse** class, and **mycircle._get_circumference** is the additional method in the **Circle** subclass.\\ \\ It is possible to create a subclass of **Circle**, thus creating a hierarchy of classes. Suppose we want to represent a circle in 2D space, which will therefore require additional data representing its position and an additional method to set the position: | ||
+ | <code bb4w> | ||
DIM CircleXY{x, y, _set_position} | DIM CircleXY{x, y, _set_position} | ||
PROC_inherit(CircleXY{}, | PROC_inherit(CircleXY{}, | ||
PROC_class(CircleXY{}) | PROC_class(CircleXY{}) | ||
DEF CircleXY._set_position (x,y) CircleXY.x = x : CircleXY.y = y : ENDPROC | DEF CircleXY._set_position (x,y) CircleXY.x = x : CircleXY.y = y : ENDPROC | ||
+ | </ | ||
Now we have an **Ellipse** -> **Circle** -> **CircleXY** class hierarchy.\\ \\ Class **inheritance** is appropriate when two classes have an '**is a**' relationship; | Now we have an **Ellipse** -> **Circle** -> **CircleXY** class hierarchy.\\ \\ Class **inheritance** is appropriate when two classes have an '**is a**' relationship; | ||
+ | <code bb4w> | ||
INSTALL @lib$+" | INSTALL @lib$+" | ||
Line 57: | Line 78: | ||
END | END | ||
+ | </ | ||
Here **Bicycle** is the container class; it can have its own members and methods. In the above example it has a constructor, | Here **Bicycle** is the container class; it can have its own members and methods. In the above example it has a constructor, |
object_20orientated_20programming.1522502370.txt.gz · Last modified: 2024/01/05 00:17 (external edit)