MATLAB Object-Oriented Programming

(Joyce) #1
if nargin == 0
data = 1;
else
mustBePositive(data)
end
obj = obj@double(data);
end
end
end

Create an object of the PositiveDouble class using a 1-by-5 array of numbers:

a = PositiveDouble(1:5);

You can perform operations on objects of this class like any double.

sum(a)

ans =

15

Objects of the PositiveDouble class must be positive values.

a = PositiveDouble(0:5);

Error using mustBePositive (line 19)
Value must be positive.

Error in PositiveDouble (line 7)
mustBePositive(data)

See Also


Related Examples



  • “Design Subclass Constructors” on page 12-9

  • “Subclasses of MATLAB Built-In Types” on page 12-50


12 How to Build on Other Classes

Free download pdf