GT AE 6382 - Programming in Matlab

Unformatted text preview:

OOP Programming in MatlabCreating a SubclassCreating the STOCK SubclassSTOCK display MethodSTOCK get MethodSTOCK set MethodSTOCK set Method (cont’d)ExamplesExample: Changing Property ValuesCreating the subsref MethodCreating the subsasgn MethodExample: Using subsrefOther Methods for STOCKWhere to from Here?Summary…SummaryFall 2006AE6382 Design Computing 1OOP Programming in Matlab•Review our ASSET class and study how it might be used as a parent for other classes•Extend the ASSET class to a subclass of STOCK•Consider others like: BONDS & SAVINGS.Learning ObjectivesWe will continue exploring objects and will construct a subclass called STOCK from the parent ASSET classTopicsFall 2006AE6382 Design Computing 2Creating a Subclass•Our ASSET class is of limited use because we will need to use it to describe many different kinds of assets–We could simply keep adding more properties to cover all situations we might encounter…–but this makes for very messy objects with lots of unused or unknown (and therefore dangerous) data–A more elegant way to handle this problem is to create a subclass of ASSET for each type of asset we might work with.•Consider a STOCK subclass of ASSET–We will want to include the number of shares and the share price–We’ll also need to update the CurrentValue property in the parent ASSET objectProperties:descriptordatecurrent_valueASSET classInherited Fields:descriptordatecurrent_valueSTOCK Fields:num_sharesshare_priceSTOCK classFall 2006AE6382 Design Computing 3Creating the STOCK Subclass•STOCK will be a subclass (child) of ASSET•Create an @STOCK directory in WORK for class filesfunction s = stock(varargin)% STOCK Stock class constructor% s=stock(descriptor, num_shares, share_price)switch nargincase 0 % no arguments so create default object s.num_shares = 0; s.share_price = 0; a = asset('none',0); % create dummy object to define parent class s = class(s,'stock',a) % create STOCK object as subclass of ASSETcase 1 % single argument so make copy if (isa(varargin{1},'stock')) s = varargin{1}; else error('STOCK: Input argument is not a STOCK object') endcase 3 s.num_shares = varargin{2}; s.share_price = varargin{3}; current_value = s.num_shares .* s.share_price; a = asset(varargin{1},current_value); % create parent object s = class(s,'stock',a); % create STOCK object as child of parentotherwise error('STOCK: Wrong number of input arguments.')enddefine the STOCK propertiesdefine the STOCK propertiescreate a “parent” objectcreate a “parent” objectnow create the STOCK “child” objectnow create the STOCK “child” objectNote: parent object must be created with specified property valuesNote: parent object must be created with specified property valuesFall 2006AE6382 Design Computing 4STOCK display Method•Just like display() for ASSETfunction display(s)% DISPLAY(a) displays a STOCK objectdisplay(s.asset) % display properties in parent objectstr = sprintf('Number of shares: %g\nShare Price: %3.2f\n',... s.num_shares, s.share_price);disp(str)First display parent property valuesFirst display parent property valuesThen display subclass property valuesThen display subclass property values•NOTE: the 3rd line shows how you access properties in another object (obj_name.obj_type)Fall 2006AE6382 Design Computing 5STOCK get Methodfunction val = get(s, prop_name)% GET: Get STOCK properties from specified objectif nargin==1 % list object’s properties if only object is given disp('STOCK properties:') disp(' Descriptor, Date, NumberShares, SharePrice, CurrentValue') returnendif ~ischar(prop_name), error('GET: prop_name must be string.'), endprop_name = lower(prop_name(isletter(prop_name))); %remove nonlettersif (length(prop_name) < 2), error('GET: prop_name must be at least 2 chars.'), endswitch prop_name(1:2)case 'nu' val = s.num_shares;case 'sh' val = s.share_price;case 'de' val = get(s.asset,'descriptor'); % call ASSET GET methodcase 'da' val = get(s.asset,'date'); % call ASSET GET methodcase 'cu' val = get(s.asset,'current_value'); % call ASSET GET methodotherwise error(['GET: ',prop_name,' is not a valid asset property.']);end Display object propertiesDisplay object propertiesget STOCK propertiesget STOCK propertiesget parent ASSET properties using its SET methodget parent ASSET properties using its SET methodFall 2006AE6382 Design Computing 6STOCK set Method•Must define which properties can be set and which will be computed•Providing error checking is IMPORTANTfunction s = set(s,varargin)% SET: set STOCK properties and return updated object.% Use: xyz=stock(xyz,'prop_name', value,...);% (Note: you must assign results to same or new object)% stock(xyz) returns list of settable properties.if nargin==1 % list settable properties if only object is given disp('STOCK properties:') disp(' Descriptor, Date, NumberShares, SharePrice, CurrentValue') returnendif rem(nargin,2) ~= 1 error('SET: prop_name, values must be provided in pairs.')endDisplay object properties if only object is givenDisplay object properties if only object is givenMake sure that proper number of arguments are provided…Make sure that proper number of arguments are provided…continued on next slideFall 2006AE6382 Design Computing 7STOCK set Method (cont’d)for k=2:2:nargin-1 prop_name = varargin{k-1}; if ~ischar(prop_name), error('SET: prop_name must be string.'), end prop_name = lower(prop_name(isletter(prop_name))); %remove nonletters value = varargin{k}; switch prop_name(1:2) case 'nu' s.num_shares = value; case 'sh' s.share_price = value; case 'de' s.asset = set(s.asset,'descriptor',value); %set parent property otherwise error('SET: invalid prop_name provided.') endend% update current value of stock in parent:value = s.num_shares .* s.share_price;s.asset = set(s.asset,'CurrentValue',value,'Date',date); Set properties in STOCK objectSet properties in STOCK objectMust compute STOCK value and then set in parent fieldMust compute STOCK value and then set in parent fieldSet property in parent ASSET objectSet property in parent ASSET objectNote that not all properties can or should be settable!Note that not all properties can or should be settable!Fall 2006AE6382 Design Computing 8Examples•Here we create a STOCK object with initial values and then use the SET method to update the price.>>


View Full Document

GT AE 6382 - Programming in Matlab

Download Programming in Matlab
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Programming in Matlab and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Programming in Matlab 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?