23.06.2015 Views

MATLAB Programming

MATLAB Programming

MATLAB Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example — The Portfolio Container<br />

• portfolio — The portfolio constructor.<br />

• display — Displays information about the portfolio contents.<br />

• pie3 — Overloaded version of pie3 function designed to take a single<br />

portfolio object as an argument.<br />

Since a portfolio object contains other objects, the portfolio class methods can<br />

use the methods of the contained objects. For example, the portfolio display<br />

method calls the stock class display method, and so on.<br />

The Portfolio Constructor Method<br />

The portfolio constructor method takes as input arguments a client’s name<br />

and a variable length list of asset subclass objects (stock, bond, and savings<br />

objects in this example). The portfolio object uses a structure array with the<br />

following fields:<br />

• name — The client’s name.<br />

• indAssets — The array of asset subclass objects (stock, bond, savings).<br />

• totalValue — The total value of all assets. The constructor calculates this<br />

value from the objects passed in as arguments.<br />

• accountNumber — The account number. This field is assigned a value only<br />

when you save a portfolio object (see “Saving and Loading Objects” on<br />

page 9-64).<br />

function p = portfolio(name,varargin)<br />

% PORTFOLIO Create a portfolio object containing the<br />

% client's name and a list of assets<br />

switch nargin<br />

case 0<br />

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

p.name = 'none';<br />

p.totalValue = 0;<br />

p.indAssets = {};<br />

p.accountNumber = '';<br />

p = class(p,'portfolio');<br />

case 1<br />

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

9-59

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

Saved successfully!

Ooh no, something went wrong!