MATLAB Object-Oriented Programming

(Joyce) #1

Example Code Discussion


d = d - 1;
end
end
str = [s{:}];
end


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


Overload disp function. Display
objects as output of char
method.

For information about this code,
see “Overload disp for
DocPolynom” on page 19-17

function dispPoly(obj,x)
p = char(obj);
e = @(x)eval(p);
y = zeros(length(x));
disp(['y = ',p])
for k = 1:length(x)
y(k) = e(x(k));
disp([' ',num2str(y(k)),...
' = f(x = ',...
num2str(x(k)),')'])
end
end


Return evaluated expression
with formatted output.

Uses output of char method to
evaluate polynomial at specified
values of independent variable.

For information about this code,
see “Display Evaluated
Expression” on page 19-18

Representing Polynomials with Classes
Free download pdf