CS1121_Matlab_BB

CS1121_Matlab_BB - Object Oriented Programming and MATLAB...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Object Oriented Programming and MATLAB Matlab programming paradiam supports OOP programming • Matlab is primarily procedural - also supports OOP • In procedural the focus is on the functions (procedures) • In OOP the focus is on OBJECS as the name suggests. • This doesn't say much unless we understand what we mean in the term Object. Structure Example: person.firstname = 'John'; person.lastname = 'Leonard'; person.address1 = '803 Shallowford Lane'; person.city = 'Peachtree City'; person.state = 'GA'; person.zip = '30269-4289'; A structure contains data in an organized fashion TRUCTURE becomes an OBJECT: person.firstname = 'John'; person.lastname = 'Leonard'; person.address1 = '803 Shallowford Lane'; person.city = 'Peachtree City'; person.state = 'GA'; person.zip = ‘30269-4289'; If we add METHODS to a structure it becomes an OBJECT: person.print_address(); person.set_lastName('Paredis'); STRUCTURE vs. OBJECT: Structs are created by specifying field names and data to be stored in these fields. Objects also have fields that store data, called properties, which operate in a very similar way. Objects unlike structs - must predefine what properties an entire class of objects has by writing a class definition. Objects like structs - have the same set of properties while the values of these properties differ just as two More Examples of Objects Car: Properties: color, speed, fuel consumption, HP… Behaviors: start, stop, turning, … Triangle: Properties: area, perimeter, linestyle, location, … Behaviors: translate, rotate, shrink, flip,… Date: Properties: year, month, day, … Behaviors: setDate, getMonth, isGreater, … More about Objects Properties: Information about the object Behaviors: Methods to set, get and process properties Encapsulation : Combining properties and behaviors in objects More about Objects Black Box Philosophy Objects perform computation by making requests of each other through the passing of Messages The only way to interact with an object is through its Methods This is called Information Hiding (the data is hidden from the user) The collection of all methods is called the Interface Another Example of an Object Bicycle Properties (Variables) Speed RPM Current Gear Methods (Functions) Braking Acceleration Turn Changing Gears These methods are inde of how the bicycle has b built (hides the implemen You can control access t members of an object Working with Objects: Messages Objects perform computation by making requests of each other through the passing of messages Parts of a message The object to which the message is addressed The name of the method to perform Any parameters needed by the method Working with Objects: Messages Different objects may respond differently to an identical message: bicycle.changeGears(lowerGear) car.changeGears(lowerGear) The same name and the same argument, but a different method = POLYMORPHISM A method is defined only in the scope of a particular type of object, called class Polymorphism is also called: function Class and Object • Now that we know what an object is all about, let's look at how we can organize these objects • Class = A blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind • Objects are individual instances of a class Class and Object • Class - House plans the architectural drawings that describe how a house is to be constructed • Instance of a Class - A House The house built from the plans is an instance of the House Class. The process of building is the Instantiation Class Object & Instance • In the class definition, we create a kind of prototype, or specification for the construction of a objects of a certain class or type. • From one class definition, we create many instances. • Objects are individual instances of a class Organizing Objects: Inheritance (Parent) Super class A class from which a particular class is derived, Sub class A class that is derived from a particular class, Inheritance promotes Reuse of code Better Management of code Children inherit • properties • behaviors Organizing Objects: Inheritance (Parent) Super class • Bicycle Sub class • Mountain Bike • Racing Bike Inheritance promotes Reuse of code Better Management of code Children inherit • properties • behaviors Abstraction in OOP • Abstraction - reveal causes and effects, expose patterns and frameworks and separate what's important from what's not • Abstraction in programming helps you make your ideas concrete in your code without obscuring the architecture with details • In procedural languages: Abstraction in OOP OOP takes abstraction one step further through: • encapsulation • data hiding • polymorphism • Inheritance Object-Oriented Programming Glossary Class - A category or set of objects Class definition file - A MATLAB file defining the behavior of a class Method - An operation that can be carried out on an object Object - An instance of a class, existing in a workspace Property - An item of data or state associated with an object Object Oriented Programming – A Quick Review: • OOP is a way of thinking not just a programmig technique • OOP is a conceptual tool that helps in analyzing the problem and solving it • Computer objects form the basis • Objects are treated as real life objects: -- Identities -- Properties -- Behaviors Object Oriented Programming – A Quick Review: (contd.) • Data and functionality encapsulated in an object • Data are hidden behind methods • Objects organized in class hierarchies: inheritance • methods – Objects interact through messages • Polymorphism: the same message has a different meaning for different objects Benefits of Object Oriented Programming • • • • • • Analysis and Design made easier Understanding of code made easier Code Reuse Ease of maintenance and enhancement Simplifies collaboration Fewer and shorter design iterations Example of a Class definition in MATLAB constructor A special method, called the constructor has to be included. The constructor is responsible for constructing new objects and it must have the same name as the class. The constructor can take any number of arguments and must return one argument, the constructed object. The constructor can specify initial values for the properties for instance. Properties can optionally be assigned default values. If no default is specified and no value is assigned by the classdef date % write a description of the class here. Properties % define the properties of the class here, (like % fields of a struct) minute = 0; hour; day; month; year; end methods % methods, including constructor defined here function obj = date(minute,hour,day,month,year) % class constructor if(nargin > 0) obj.minute = minute; obj.hour = hour; obj.day = day; obj.month = month; obj.year = year; end end function obj = rollDay(obj,numdays) obj.day = obj.day + numdays; end end Now that we have written the class, we can create date objects by simply calling the constructor. d1 = date(0,3,27,2,1998); %Creates a date object The corresponding properties will be set. d2 = date(); % create a new date object None of the properties, (except for minute) will be set. (They will all be equal to ). Properties • Properties defined as public are accessible from outside the class just like fields of a struct. We can access and assign them using dot notation. day = d1.day; % access the day property d1.year = 2008; % set the year property • It is usually a good idea, however, to restrict direct access to properties to Properties • we want to allow use of a class without providing details of implementation which will give us the freedom of how we change and store properties. • For example, we may want to make changes to only calculate the value of a property when the user asks for it, (so called lazy evaluation). Without a level of indirection, this would not be possible. • The idea is then to provide access to properties but only through methods. Properties Matlab offers control over property access. There are three levels, private , protected , and public and these apply separately to read and write access. ---Private properties are only accessible from methods of the class, ---Public properties, (the default) are accessible anywhere. Methods • Methods in OOP are functions, defined in a class, that operate specifically on objects of that class, (although they can certainly take objects of other classes as additional arguments). Methods • Observe the simple function rollDay() % defined earlier This method adds a specified number of days to the day property. Notice that the first argument of the function definition is an object of the containing class. • There are two equivalent ways of calling methods: d1 = rollDay(d,3); d1 = d.rollDay(3); • The second option is similar to java and C++. • Note, the method takes 2 parameters in both cases, (i.e. nargin() is 2 in each). There is really no particular reason to choose one over the other. Pick Objects in Matlab are by default, passed by value not by reference • We must make sure to return the object when we modify any properties. Objects in Matlab are by default, passed by value not by reference, which means that a full copy of the object is passed to methods and it is this copy that is modified. If we do not pass back this copy, effectively overwriting the original, we will not observe any change in the state. We can optionally write classes whose objects are passed by reference. Classes in MATLAB • In MATLAB, the class of a variable is displayed in the output of the whos command, together with other variable characteristics. • Examples include double & char. • >> a=1; >> str=’Hello’; >> whos Name Size Bytes Class Attributes a 1x1 8 double str 1x5 10 char Classes in MATLAB Examples of classes in MATLAB. • >>M =[1 3 4 ; 5 6 7] M is an instance of class double • >> c=[ {M}; {pi}; {[ ]} ] c is an instance of class cell ...
View Full Document

Ask a homework question - tutors are online