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 />

The stock class display method can now call this method to display the data<br />

stored in the parent class. This approach isolates the stock display method<br />

from changes to the asset class. See “The Stock display Method” on page 9-57<br />

for an example of how this method is called.<br />

TheAssetfieldcountMethod<br />

The asset fieldcount method returns the number of fields in the asset object<br />

data structure. fieldcount enables asset child methods to determine the<br />

number of fields in the asset object during execution, rather than requiring<br />

thechildmethodstohaveknowledgeoftheassetclass. Thisallowsyouto<br />

make changes to the number of fields in the asset class data structure without<br />

having to change child-class methods.<br />

function numFields = fieldcount(assetObj)<br />

% Determines the number of fields in an asset object<br />

% Used by asset child class methods<br />

numFields = length(fieldnames(assetObj));<br />

The struct function converts an object to its equivalent data structure,<br />

enabling access to the structure’s contents.<br />

Designing the Stock Class<br />

A stock object is designed to represent one particular asset in a person’s<br />

investment portfolio. This object contains two properties of its own and<br />

inherits three properties from its parent asset object.<br />

Stock properties:<br />

• NumberShares — The number of shares for the particular stock object.<br />

• SharePrice — The value of each share.<br />

Asset properties:<br />

• Descriptor — The identifier of the particular asset (e.g., stock name,<br />

savings account number, etc.).<br />

• Date — The date the object was created (calculated by the date command).<br />

• CurrentValue — The current value of the asset.<br />

9-49

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

Saved successfully!

Ooh no, something went wrong!