<html lang="en"> <head> <title>Creating a Class - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Object-Oriented-Programming.html#Object-Oriented-Programming" title="Object Oriented Programming"> <link rel="next" href="Manipulating-Classes.html#Manipulating-Classes" title="Manipulating Classes"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Creating-a-Class"></a> <p> Next: <a rel="next" accesskey="n" href="Manipulating-Classes.html#Manipulating-Classes">Manipulating Classes</a>, Up: <a rel="up" accesskey="u" href="Object-Oriented-Programming.html#Object-Oriented-Programming">Object Oriented Programming</a> <hr> </div> <h3 class="section">33.1 Creating a Class</h3> <p>We use in the following text a polynomial class to demonstrate the use of object oriented programming within Octave. This class was chosen as it is simple, and so doesn't distract unnecessarily from the discussion of the programming features of Octave. However, even still a small understand of the polynomial class itself is necessary to fully grasp the techniques described. <p>The polynomial class is used to represent polynomials of the form <pre class="example"> a0 + a1 * x + a2 * x^2 + ... + an * x^n </pre> <p class="noindent">where a0, a1, etc. are real scalars. Thus the polynomial can be represented by a vector <pre class="example"> a = [a0, a1, a2, ..., an]; </pre> <p>We therefore now have sufficient information about the requirements of the class constructor for our polynomial class to write it. All object oriented classes in Octave, must be contained with a directory taking the name of the class, prepended with the @ symbol. For example, with our polynomial class, we would place the methods defining the class in the @polynomial directory. <p>The constructor of the class, must have the name of the class itself and so in our example the constructor with have the name <samp><span class="file">@polynomial/polynomial.m</span></samp>. Also ideally when the constructor is called with no arguments to should return a value object. So for example our polynomial might look like <pre class="example"><pre class="verbatim"> ## -*- texinfo -*- ## @deftypefn {Function File} {} polynomial () ## @deftypefnx {Function File} {} polynomial (@var{a}) ## Creates a polynomial object representing the polynomial ## ## @example ## a0 + a1 * x + a2 * x^2 + @dots{} + an * x^n ## @end example ## ## from a vector of coefficients [a0 a1 a2 ... an]. ## @end deftypefn function p = polynomial (a) if (nargin == 0) p.poly = [0]; p = class (p, "polynomial"); elseif (nargin == 1) if (strcmp (class (a), "polynomial")) p = a; elseif (isvector (a) && isreal (a)) p.poly = a(:).'; p = class (p, "polynomial"); else error ("polynomial: expecting real vector"); endif else print_usage (); endif endfunction </pre> </pre> <p>Note that the return value of the constructor must be the output of the <code>class</code> function called with the first argument being a structure and the second argument being the class name. An example of the call to this constructor function is then <pre class="example"> p = polynomial ([1, 0, 1]); </pre> <p>Note that methods of a class can be documented. The help for the constructor itself can be obtained with the constructor name, that is for the polynomial constructor <code>help polynomial</code> will return the help string. Also the help can be obtained by restricting the search for the help to a particular class, for example <code>help @polynomial/polynomial</code>. This second method is the only means of getting help for the overloaded methods and functions of the class. <p>The same is true for other Octave functions that take a function name as an argument. For example <code>type @polynomial/display</code> will print the code of the display method of the polynomial class to the screen, and <code>dbstop @polynomial/display</code> will set a breakpoint at the first executable line of the display method of the polynomial class. <p>To check where a variable is a user class, the <code>isobject</code> and <code>isa</code> functions can be used. for example <pre class="example"> p = polynomial ([1, 0, 1]); isobject (p) ⇒ 1 isa (p, "polynomial") ⇒ 1 </pre> <!-- ov-class.cc --> <p><a name="doc_002disobject"></a> <div class="defun"> — Built-in Function: <b>isobject</b> (<var>x</var>)<var><a name="index-isobject-2254"></a></var><br> <blockquote><p>Return true if <var>x</var> is a class object. </p></blockquote></div> <p class="noindent">The available methods of a class can be displayed with the <code>methods</code> function. <!-- ov-class.cc --> <p><a name="doc_002dmethods"></a> <div class="defun"> — Built-in Function: <b>methods</b> (<var>x</var>)<var><a name="index-methods-2255"></a></var><br> — Built-in Function: <b>methods</b> (<var>"classname"</var>)<var><a name="index-methods-2256"></a></var><br> <blockquote><p>Return a cell array containing the names of the methods for the object <var>x</var> or the named class. </p></blockquote></div> <p class="noindent">To inquire whether a particular method is available to a user class, the <code>ismethod</code> function can be used. <!-- ov-class.cc --> <p><a name="doc_002dismethod"></a> <div class="defun"> — Built-in Function: <b>ismethod</b> (<var>x, method</var>)<var><a name="index-ismethod-2257"></a></var><br> <blockquote><p>Return true if <var>x</var> is a class object and the string <var>method</var> is a method of this class. </p></blockquote></div> <p class="noindent">For example <pre class="example"> p = polynomial ([1, 0, 1]); ismethod (p, "roots") ⇒ 1 </pre> </body></html>