Programming |
The stock constructor creates a stock object from three input arguments:
The constructor must create an asset object from within the stock constructor to be able to specify it as a parent to the stock object. The stock constructor must, therefore, call the asset constructor. The class
function, which is called to create the stock object, defines the asset object as the parent.
Keep in mind that the asset object is created in the temporary workspace of the stock constructor function and is stored as a field (.asset
) in the stock structure. The stock object inherits the asset fields, but the asset object is not returned to the base workspace.
function s = stock(varargin) % STOCK Stock class constructor. % s = stock(descriptor, numShares, sharePrice) switch nargin case 0 % if no input arguments, create a default object s.numShares = 0; s.sharePrice = 0; a = asset('none',0); s = class(s, 'stock',a); case 1 % if single argument of class stock, return it if (isa(varargin{1},'stock')) s = varargin{1}; else error('Input argument is not a stock object') end case 3 % create object using specified values s.numShares = varargin{2}; s.sharePrice = varargin{3}; a = asset(varargin{1},'stock',varargin{2} * varargin{3}); s = class(s,'stock',a); otherwise error('Wrong number of input arguments') end
Constructor Calling Syntax
The stock constructor method can be called in one of three ways:
Otherwise, if none of the above three conditions are met, return an error.
For example, this statement creates a stock object to record the ownership of 100 shares of XYZ corporation stocks with a price per share of 25 dollars.
Designing the Stock Class | The Stock get Method |
© 1994-2005 The MathWorks, Inc.