Next: Class Methods, Up: Object Oriented Programming [Contents][Index]
This chapter illustrates user-defined classes and object oriented programming through a custom class designed for polynomials. This class was chosen for its simplicity which does not distract unnecessarily from the discussion of the programming features of Octave. Even so, a bit of background on the goals of the polynomial class is necessary before the syntax and techniques of Octave object oriented programming are introduced.
The polynomial class is used to represent polynomials of the form
a0 + a1 * x + a2 * x^2 + … + an * x^n
where a0, a1, etc. are real scalars. Thus the polynomial can be represented by a vector
a = [a0, a1, a2, …, an];
This is a sufficient specification to begin writing the constructor for the polynomial class. All object oriented classes in Octave must be located in a directory that is the name of the class prepended with the ‘@’ symbol. For example, the polynomial class will have all of its methods defined in the @polynomial directory.
The constructor for the class must be the name of the class itself; in this example the constructor resides in the file @polynomial/polynomial.m. Ideally, even when the constructor is called with no arguments it should return a valid object. A constructor for the polynomial class might look like
## -*- texinfo -*- ## @deftypefn {} {} polynomial () ## @deftypefnx {} {} polynomial (@var{a}) ## Create a polynomial object representing the polynomial ## ## @example ## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n ## @end example ## ## @noindent ## from a vector of coefficients [a0 a1 a2 @dots{} an]. ## @end deftypefn function p = polynomial (a) if (nargin > 1) print_usage (); endif if (nargin == 0) p.poly = 0; p = class (p, "polynomial"); else if (isa (a, "polynomial")) p = a; elseif (isreal (a) && isvector (a)) p.poly = a(:).'; # force row vector p = class (p, "polynomial"); else error ("@polynomial: A must be a real vector"); endif endif endfunction
Note that the return value of the constructor must be the output of the
class
function. The first argument to the class
function is a
structure and the second is the name of the class itself. An example of
calling the class constructor to create an instance is
p = polynomial ([1, 0, 1]);
Methods are defined by m-files in the class directory and can have embedded
documentation the same as any other m-file. The help for the constructor can
be obtained by using the constructor name alone, that is, for the polynomial
constructor help polynomial
will return the help string. Help can be
restricted to a particular class by using the class directory name followed
by the method. For example, help @polynomial/polynomial
is another
way of displaying the help string for the polynomial constructor. This second
means is the only way to obtain help for the overloaded methods and functions
of a class.
The same specification mechanism can be used wherever Octave expects a function
name. For example type @polynomial/disp
will print the code of the
disp
method of the polynomial class to the screen, and
dbstop @polynomial/disp
will set a breakpoint at the first executable
line of the disp
method of the polynomial class.
To check whether a variable belongs to a user class, the isobject
and
isa
functions can be used. For example:
p = polynomial ([1, 0, 1]); isobject (p) ⇒ 1 isa (p, "polynomial") ⇒ 1
The available methods of a class can be displayed with the methods
function.
List the names of the public methods for the object obj or the named class classname.
obj may be an Octave class object or a Java object. classname may be the name of an Octave class or a Java class.
When called with no output arguments, methods
prints the list of
method names to the screen. Otherwise, the output argument mtds
contains the list in a cell array of strings.
See also: fieldnames.
To inquire whether a particular method exists for a user class, the
ismethod
function can be used.
Return true if the string method is a valid method of the object obj or of the class clsname.
For a polynomial class it makes sense to have a method to compute its roots.
function r = roots (p) r = roots (fliplr (p.poly)); endfunction
We can check for the existence of the roots
-method by calling:
p = polynomial ([1, 0, 1]); ismethod (p, "roots") ⇒ 1
Next: Class Methods, Up: Object Oriented Programming [Contents][Index]