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.

9 Classes and Objects<br />

Note that the property names are not actually the same as the field names of<br />

the structure array used internally by stock and asset objects. The property<br />

name interface is controlled by the stock and asset set and get methods and<br />

is designed to resemble the interface of other <strong>MATLAB</strong> object properties.<br />

The asset field in the stock object structure contains the parent asset object<br />

and is used to access the inherited fields in the parent structure.<br />

Stock Class Methods<br />

The stock class implements the following methods:<br />

• Constructor<br />

• get and set<br />

• subsref and subsasgn<br />

• display<br />

The Stock Constructor Method<br />

The stock constructor creates a stock object from three input arguments:<br />

• The stock name<br />

• The number of shares<br />

• The share price<br />

The constructor must create an asset object from within the stock constructor<br />

to be able to specify it as a parent to the stock object. The stock constructor<br />

must, therefore, call the asset constructor. The class function, which is called<br />

to create the stock object, defines the asset object as the parent.<br />

Keep in mind that the asset object is created in the temporary workspace of<br />

the stock constructor function and is stored as a field (.asset) inthestock<br />

structure. The stock object inherits the asset fields, but the asset object is<br />

not returned to the base workspace.<br />

function s = stock(varargin)<br />

% STOCK Stock class constructor.<br />

% s = stock(descriptor, numShares, sharePrice)<br />

9-50

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

Saved successfully!

Ooh no, something went wrong!