OOP: Creating a Class Topics More OOP concepts

Slides:



Advertisements
Similar presentations
Final and Abstract Classes
Advertisements

Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Composition CMSC 202. Code Reuse Effective software development relies on reusing existing code. Code reuse must be more than just copying code and changing.
Fall 2006AE6382 Design Computing1 Object Oriented Programming in Matlab Intro to Object- Oriented Programming (OOP) An example that creates a ASSET class.
CS 106 Introduction to Computer Science I 04 / 11 / 2008 Instructor: Michael Eckmann.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Encapsulation, Inheritance & Interfaces CSE 115 Spring 2006 February 27, March 1 & 3, 2006.
CS 106 Introduction to Computer Science I 04 / 16 / 2010 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 15 / 2006 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 23 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 11 / 19 / 2007 Instructor: Michael Eckmann.
Program design example Task: Develop an algorithm expressed in pseudocode for a specified problem specified problem.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Programming Languages and Paradigms Object-Oriented Programming.
CSM-Java Programming-I Spring,2005 Introduction to Objects and Classes Lesson - 1.
CS 106 Introduction to Computer Science I 04 / 13 / 2007 Friday the 13 th Instructor: Michael Eckmann.
Fall 2006AE6382 Design Computing1 OOP: Creating a Class More OOP concepts An example that creates a ASSET class and shows how it might be used Extend the.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Digital Image Processing Lecture10: MATLAB Example – Utility M-functions for Intensity Transformations.
Advanced Topics- Functions Introduction to MATLAB 7 Engineering 161.
Chapter 12 Support for Object oriented Programming.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Programming in Java CSCI-2220 Object Oriented Programming.
Objects and Classes UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
IT108 Objects and Classes Part I George Mason University Revised 4/3/2012.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Fall 2006AE6382 Design Computing1 OOP Programming in Matlab Review our ASSET class and study how it might be used as a parent for other classes Extend.
1 C# - Inheritance and Polymorphism. 2 1.Inheritance 2.Implementing Inheritance in C# 3.Constructor calls in Inheritance 4.Protected Access Modifier 5.The.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2016.
Lecture 3 (UNIT -1) SUNIL KUMAR CIT-UPES.
User-Written Functions
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Winter 2013.
Static data members Constructors and Destructors
Objects as a programming concept
Classes and OOP.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2017.
Final and Abstract Classes
Inheritance and Polymorphism
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2017.
Scripts & Functions Scripts and functions are contained in .m-files
User Defined Functions
Phil Tayco Slide version 1.0 Created Oct 2, 2017
Topics Introduction to File Input and Output
Properties 7: Properties Programming C# © 2003 DevelopMentor, Inc.
Java Programming Language
Phil Tayco Slide version 1.0 Created Oct 9, 2017
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Autumn 2018.
Object Oriented Programming
Coding Concepts (Basics)
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Zach Tatlock Winter 2018.
CSE341: Programming Languages Section 9 Dynamic Dispatch Manually in Racket Zach Tatlock Winter 2018.
Classes and Objects.
Java – Inheritance.
Java Programming, Second Edition
CISC/CMPE320 - Prof. McLeod
Inheritance.
Java Programming Language
Data Structures & Algorithms
Review of Previous Lesson
Review of Previous Lesson
Review: libraries and packages
Final and Abstract Classes
Topics Introduction to File Input and Output
CMPE212 – Reminders Assignment 2 due next Friday.
CSE341: Programming Languages Lecture 21 Dynamic Dispatch Precisely, and Manually in Racket Dan Grossman Spring 2019.
Presentation transcript:

OOP: Creating a Class Topics 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.

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) ARRAY char NUMERIC cell structure java class function handle user class int8, uint8, int16, uint16, int32, uint32 single double sparse

Let’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 value ASSET class Superclass (Parent) Properties: descriptor date current_value Subclass (Child) Inherited Fields: descriptor date current_value STOCK Fields: num_shares share_price asset STOCK class Inherited Fields: descriptor date current_value BOND Fields: int_rate asset BOND class Inherited Fields: descriptor date current_value SAVINGS Fields: int_rate asset SAVINGS class Inherited Fields: descriptor date current_value PROP Fields: mort_rate asset PROP class

Creating 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 done

ASSET Constructor function 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 nargin case 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 object case 1 % one arg means return same if (isa(varargin{1},'asset')) a = varargin{1}; else error ('ASSET: single argument must be of asset class.') end case 2 % create object using specified values a.descriptor = varargin{1}; a.current_value = varargin{2}; a = class(a,'asset'); % create the object otherwise error('ASSET: wrong number of arguments.') varargin is a cell array with all the nargin arguments a is a structure where: a.date=date string All constructors must handle these 3 cases Polymorphism: different use of class() in constructor

Displaying 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… There are lots of possible variations depending on how you want the display to look… function display(a) % DISPLAY(a) displays an ASSET object str = sprintf('Descriptor: %s\nDate: %s\nCurrent Value:%9.2f',... a.descriptor, a.date, a.current_value); disp(str) - Example showing how display() is used if the semicolon is omitted in a command line >> a=asset('My asset', 1000) Descriptor: My asset Date: 29-Nov-2001 Current Value: 1000.00

Comments 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).

Examples 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 asset Date: 10-Oct-2002 Current Value: 1000.00 >> b=asset(a); >> whos Name Size Bytes Class a 1x1 418 asset object b 1x1 418 asset object Grand total is 92 elements using 2428 bytes >> c=asset Descriptor: none Current Value: 0.00 Creates an object Creates a copy of a Creates an empty object

Our Class has no Methods yet… Arithmetic operations are not defined >> a+b ??? Error using ==> + Function '+' not defined for variables of class 'asset'. >> x=a(1); >> x Descriptor: My asset Date: 29-Nov-2001 Current Value: 1000.00 >> class(x) ans = asset >> whos Name Size Bytes Class a 1x1 418 asset object b 1x1 418 asset object c 1x1 410 asset object x 1x1 418 asset object Grand total is 88 elements using 1664 bytes Using an index simply references the entire object (x is a copy of a)

Listing (getting) Property Values from Object We will want to extract property values from an object GET is a “standard” name function val = get(a, prop_name) % GET: Get asset properties from specified object if ~ischar(prop_name), error('GET: prop_name must be string.'), end prop_name = lower(prop_name(isletter(prop_name))); %remove nonletters if (length(prop_name) < 2) error('GET: prop_name must be at least 2 chars.') end switch prop_name(1:2) case 'de' val = a.descriptor; case 'da' val = a.date; case 'cu' val = a.current_value; otherwise error(['GET: ',prop_name,' is not a valid asset property.']); Must check for valid input Note the tricky way to remove all nonletters in the string ! You can test for some or all of the property name string (shortens how much the user must type)

Changing Property Values in an Object W must be able to change the values of some of the properties in the object: we’ll create a method (function) called get() for this it will be part of the asset class located in the @ASSET directory function a = set(a,varargin) % SET: set asset properties and return updated object. More than one % property can be set but must provide: ‘prop_name’, value pairs if rem(nargin,2) ~= 1 error('SET: prop_name, values must be provided in pairs.') end 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 'de' a.descriptor = value; case 'da' a.date = value; case 'cu' a.current_value = value; otherwise error('SET: invalid prop_name provided.') NOTE: you should include code to check for proper values as well.

Example using get and set 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 Descriptor: My asset Date: 10-Oct-2002 Current Value: 1000.00 >> get(a,'Descriptor') ans = My asset >> a=set(a,'CurrentValue',2000) Current Value: 2000.00 >> a=set(a,'Date',datestr(datenum(date)+1)) Date: 11-Oct-2002 Get a property value Set a property value Change date…

Important Notes We didn’t need a return value using get, but… We MUST show the same object we’re modifying as the return value for set this is because Matlab does not allow us to modify the values of the arguments in a function and have them returned to the calling workspace!!! We could use the assignin() function to avoid this problem (see also the evalin() function for another variation on this) Other OOP languages don’t have this problem… This is perhaps the most awkward, inelegant aspect of OOP in Matlab. It is VERY non-standard when compared to Java, C++ or SmallTalk, etc. set() must return the same object as provided in the argument list >> a=set(a,'CurrentValue',2000) Descriptor: My asset Date: 29-Nov-2001 Current Value: 2000.00

Summary… OOP has let us create an entirely new data type in Matlab this is done by creating a class to define this data type we must also create all the methods (functions) that can be used with this special data type the internal details are hidden inside the class What else could we do with this new class? create a subclass of assets called STOCK that will include fields for data specific to a stock asset (see next slide) the methods for this new subclass will be in the @STOCK subdirectory we could continue to create subclasses for BOND and PROPERTY types of assets, each with their own extended data values and perhaps new methods. AE6382 Design Computing Fall 2006

Where to from here? With only a little modification we can create an entirely new subclass that is based on our STOCK class We can do this by specifying that our new class will inherit the structure and methods from a parent or superclass (or from several parent classes). This is called “inheritance” and is an important feature of OOP. Consider a BOND class It has an identical structure to a STOCK, But there will be different properties We will create new display, get and set methods We will use the set and get methods from ASSET to modify properties in the parent

Summary Learning Objectives Action Items OOP concepts ASSET & STOCK classes Summary Action Items Review the lecture Perform the exercises See if you can create the BOND class and get it working like we did for the STOCK class. Consider how this might be extended to other applications