23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example — Assets and Asset Subclasses<br />

switch nargin<br />

case 0<br />

% if no input arguments, create a default object<br />

s.numShares = 0;<br />

s.sharePrice = 0;<br />

a = asset('none',0);<br />

s = class(s,'stock',a);<br />

case 1<br />

% if single argument of class stock, return it<br />

if (isa(varargin{1},'stock'))<br />

s = varargin{1};<br />

else<br />

error('Input argument is not a stock object')<br />

end<br />

case 3<br />

% create object using specified values<br />

s.numShares = varargin{2};<br />

s.sharePrice = varargin{3};<br />

a = asset(varargin{1},'stock',varargin{2} * varargin{3});<br />

s = class(s,'stock',a);<br />

otherwise<br />

error('Wrong number of input arguments')<br />

end<br />

Constructor Calling Syntax<br />

The stock constructor method can be called in one of three ways:<br />

• No input argument — If called with no arguments, the constructor returns<br />

a default object with empty fields.<br />

• Inputargumentisastockobject—Ifcalledwithasingleinputargument<br />

that is a stock object, the constructor returns the input argument. A single<br />

argument that is not a stock object generates an error.<br />

• Three input arguments — If there are three input arguments, the<br />

constructor uses them to define the stock object.<br />

Otherwise, if none of the above three conditions are met, return an error.<br />

9-51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!