MATLAB Object-Oriented Programming

(Joyce) #1

Example Code Discussion


function obj = DocPolynom(c)
if nargin > 0
if isa(c,'DocPolynom')
obj.coef = c.coef;
else
obj.coef = c(:).';
end
end
end


Class constructor creates objects
using:


  • Coefficient vector of existing
    object

  • Coefficient vector passed as
    argument


See “The DocPolynom
Constructor” on page 19-13

function obj = set.coef(obj,val)
if ~isa(val,'double')
error('Coefficients must be doubles')
end
ind = find(val(:).'~=0);
if ~isempty(ind);
obj.coef = val(ind(1):end);
else
obj.coef = val;
end
end


Set method for coef property:


  • Allows coefficients only of
    type double

  • Removes leading zeros from
    the coefficient vector.


See “Remove Irrelevant
Coefficients” on page 19-14

function c = double(obj)
c = obj.coef;
end


Convert DocPolynom object to
double by returning the
coefficients.

See “Convert DocPolynom
Objects to Other Types” on page
19-15

Representing Polynomials with Classes
Free download pdf