Unformatted text preview:

OOP: Creating a ClassMatlab’s Built-in ClassesLet’s create a Matlab class called ASSETCreating the Object: ConstructorsASSET ConstructorDisplaying the Object…CommentsExamples with our new classOur Class has no Methods yet…Listing (getting) Property Values from ObjectChanging Property Values in an ObjectExample using get and setImportant NotesSummary…Where to from here?SummaryFall 2006AE6382 Design Computing 1OOP: Creating a Class•More OOP concepts•An example that creates a ASSET class and shows how it might be used•Extend the ASSET class to subclasses of STOCKS, BONDS and SAVINGS.Learning ObjectivesWe’re now going to introduce you to some advanced computing concepts, and we’ll illustrate a simple application that creates a new data type called an ASSET with subtypes.TopicsFall 2006AE6382 Design Computing 2Matlab’s Built-in Classes•Matlab’s built-in classes:–others in additional Toolboxes•You can define your own Matlab classes:–data structure (the fields) is a Matlab struct–methods (functions) are stored in special directory (@classname) that is a subdirectory of a directory in the Matlab path–a constructor is a method that is used to create an object (an instance of a class)char NUMERIC javaclasscell functionhandleint8, uint8,int16, uint16,int32, uint32single doublesparseuser classstructureARRAYFall 2006AE6382 Design Computing 3Let’s create a Matlab class called ASSET•What is an ASSET?–it is an item with a monetary value–examples could be a stock, or a bond, or a savings account–it could also be a physical item with a monetary value (car, house)–each asset has a different way of defining its monetary valueProperties:descriptordatecurrent_valueASSET classInherited Fields:descriptordatecurrent_valueSTOCK Fields:num_sharesshare_priceassetSTOCK classInherited Fields:descriptordatecurrent_valueBOND Fields:int_rateassetBOND classInherited Fields:descriptordatecurrent_valueSAVINGS Fields:int_rateassetSAVINGS classInherited Fields:descriptordatecurrent_valuePROP Fields:mort_rateassetPROP classSuperclass (Parent)Subclass (Child)Fall 2006AE6382 Design Computing 4Creating the Object: Constructors•An object must first be created using the “definition” provided in the Class (this is called instantiation)–this is like defining a new double variable (e.g., A(3,1)=5.4)–the created object now has access to all the methods that are defined in the class from which it was created–OOP languages have sophisticated ways to keep these methods and data known only inside the object or shared with one or more other classes of objects (Matlab is very simple as we’ll see later)–we’ll need a special method called a constructor to create an object (all classes must define a constructor method)–in some OOP languages, you must also provide a destructor method to remove an object when you’re doneFall 2006AE6382 Design Computing 5ASSET Constructorfunction a=asset(varargin)% ASSET constructor function for asset object% Use: a = asset(descriptor, current_value)% Fields:% descriptor = string% date = date string (automatically set to current)% current_value = asset value ($)switch nargincase 0 % no arguments: create a default object a.descriptor = 'none'; a.date = date; a.current_value = 0; a = class(a,'asset'); % this actually creates the objectcase 1 % one arg means return same if (isa(varargin{1},'asset')) a = varargin{1}; else error ('ASSET: single argument must be of asset class.') endcase 2 % create object using specified values a.descriptor = varargin{1}; a.date = date; a.current_value = varargin{2}; a = class(a,'asset'); % create the objectotherwise error('ASSET: wrong number of arguments.')enda is a structure where:a.date=date stringa is a structure where:a.date=date stringvarargin is a cell array with all the nargin argumentsvarargin is a cell array with all the nargin argumentsAll constructors must handle these 3 casesAll constructors must handle these 3 casesPolymorphism: different use of class() in constructorPolymorphism: different use of class() in constructorFall 2006AE6382 Design Computing 6Displaying the Object…•We need to add a method to display the object–Matlab always calls display for this purpose–we must add a method in our ASSET class…function display(a)% DISPLAY(a) displays an ASSET objectstr = sprintf('Descriptor: %s\nDate: %s\nCurrent Value:%9.2f',... a.descriptor, a.date, a.current_value);disp(str)>> a=asset('My asset', 1000)Descriptor: My assetDate: 29-Nov-2001Current Value: 1000.00- Example showing how display() is used if the semicolon is omitted in a command lineThere are lots of possible variations depending on how you want the display to look…There are lots of possible variations depending on how you want the display to look…Fall 2006AE6382 Design Computing 7Comments•Every class must define a constructor and a display method (or it must be inherited from a superclass)–by convention, the constructor should handle at least the following 3 situations:–create an empty object if no arguments are supplied, or–make a copy of the object if one is supplied as the only argument. or–create a new object with specified properties.•The class() function (method) is polymorphic:–class(A) if used anywhere will return the type of variable A–class(a,’asset’) is only allowed inside a constructor and it tags a as being of class (type) ‘asset’–class(a,’asset’,parent1) tags a as above and also it inherits methods and fields from a parent1 object (class).Fall 2006AE6382 Design Computing 8Examples with our new class•Try the following examples:–Make sure you have created a subdirectory called @ASSET in the Matlab work directory (or a directory in the Matlab path)–copy the functions we’ve described into this directory–type in the examples at the Command prompt…>> a=asset('My asset', 1000)Descriptor: My assetDate: 10-Oct-2002Current Value: 1000.00>> b=asset(a);>> whos Name Size Bytes Class a 1x1 418 asset object b 1x1 418 asset objectGrand total is 92 elements using 2428 bytes>> c=assetDescriptor: noneDate: 10-Oct-2002Current Value: 0.00Creates an empty objectCreates an empty objectCreates an objectCreates an objectCreates a copy of aCreates a copy of aFall 2006AE6382 Design Computing 9Our Class has no Methods yet…>> a+b??? Error using ==> +Function '+' not defined


View Full Document

GT AE 6382 - Creating a Class

Download Creating a Class
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 Creating a Class 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 Creating a Class 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?