MATLAB Object-Oriented Programming

(Joyce) #1

case 'disp'
disp(a)
otherwise
if length(s)>1
b = a.(s(1).subs)(s(2).subs{:});
else
b = a.(s.subs);
end
end
otherwise
error('Specify value for x as obj(x)')
end
end
end


Define Arithmetic Operators


Several arithmetic operations are meaningful on polynomials. The DocPolynom class
implements these methods:


Method and Syntax Operator Implemented
plus(a,b) Addition
minus(a,b) Subtraction
mtimes(a,b) Matrix multiplication

When overloading arithmetic operators, consider the data types you must support. The
plus, minus, andmtimes methods are defined for the DocPolynom class to handle
addition, subtraction, and multiplication on DocPolynom — DocPolynom and
DocPolynom — double combinations of operands.


Define + Operator


If either p or q is a DocPolynom object, this expression:


p + q


Generates a call to a function @DocPolynom/plus, unless the other object is of higher
precedence.


The following method overloads the plus (+) operator for the DocPolynom class:


methods
function r = plus(obj1,obj2)


Representing Polynomials with Classes
Free download pdf