MATLAB Object-Oriented Programming

(Joyce) #1

if d > 0
s(ind) = {'*'};
ind = ind + 1;
end
end
end
if d >= 2
s(ind) = {['x^' int2str(d)]};
ind = ind + 1;
elseif d == 1
s(ind) = {'x'};
ind = ind + 1;
end
end
d = d - 1;
end
end
str = [s{:}];
end
end


Overload disp for DocPolynom


To provide a more useful display of DocPolynom objects, this class overloads disp in the
class definition.


This disp method relies on the char method to produce a text representation of the
polynomial, which it then displays on the screen.


The char method returns a cell array or the character '0' if the coefficients are all zero.


methods
function disp(obj)
c = char(obj);
if iscell(c)
disp([' ' c{:}])
else
disp(c)
end
end
end


When MATLAB Calls the disp Method


The statement:


p = DocPolynom([1 0 -2 -5])


Representing Polynomials with Classes
Free download pdf