![]() |
Exploiting MATLAB's Data Structures |
The object oriented extensions built into MATLAB allow you to overload common syntax such as a + b to do anything you want! In this problem, you will create a roman object that can add two roman numbers.
The object oriented extensions in MATLAB hinge on the concept of an object variable. object variables are like normal variables that live in the workspace except that they can have special m-files defined for them called methods. When an object variable is used as an input argument to a function, MATLAB will look for and find a method with the same name as the function if it exists.
Methods must be placed into a directory with the same name as the class of the object is goes with preceeded by an '@' sign. For example, the methods for the roman object you will create should live in a directory called @roman.
You can overload the operators involved in expression as well by creating
methods with special names. For instance to overload the '+' operator
you must create a method with the name plus.m that takes two input
arguments and returns a single result (for the list of special names see
help datatypes).
The constructor method for an object is a method with the same name of as the class. The constuctor's job is to create the object so this is where you must define and initialize the object's fields. An object is first created as a structure and then passed through the class function to turn it into an object:
function obj = myclass(x) %MYCLASS Constructor for the MYCLASS object. % OBJ = MYCLASS(X) returns a MYCLASS object with value X. obj.dat = x; obj = class(obj,'myclass');
This method, when placed into a directory called @myclass, can be used to create a myclass variable.
Goal: Create a constructor and overloaded method for plus.
Key concepts: object oriented programming, overloading mathematical syntax, constructors, method directories, creating directories with Windows 95
Key functions: class, romancalc, methods
romancalc
roman(v). The
roman object should have one field, val, that contains
the decimal value of the object. A few other methods have already been
created for you that use this field to do their work (you can use methods
roman to see their names)
plus to add two roman numerals.
mtimes to multiply
two roman numerals
![]()
Copyright © 1997 The MathWorks, Inc.
clay@mathworks.com
$Revision$ $Date$