You are on page 1of 3

CS1112Lecture22

4/10/2013

Previous lecture:
Introduction to objects and classes

Object-Oriented Programming First design and define the classes (of the objects)
Identify the properties (data) and actions (methods, i.e., functions) of each class

Todays lecture:
Defining a class
Constructor Methods

Objects are passed by reference to functions Overloading methods

Announcements:
Prelim 2 will be on Tues

Then create the objects (from the classes) that are then used, that interact with one another

classdef Interval < handle

Class Interval An interval has two properties:


left, right

Class Interval
left, right

An interval has two properties:

properties left right end methods function scale(self, f) ... end function shift(self, s)

Actionsmethodsof an interval include


Scale, i.e., expand Shift Add one interval to another Check if one interval is in another Check if one interval overlaps with another
SeedemoInterval1.m

Actionsmethodsof an interval include


... Scale, i.e., expand end Shift function Inter = overlap(self, other) ... Add one interval to another end function Inter = add(self, other) Check if one interval is in another ... end Check if one interval overlaps with another ... end end

Tospecifythepropertiesandactions ofanobjectistodefine itsclass

An Interval object
167.32 Thehandle orreference oftheobject

classdef Interval < handle properties left right end methods function scale(self, f) ... end function shift(self, s) ... end function Inter = overlap(self, other) ... end function Inter = add(self, other) ... end

The constructor method To create an Interval object, use its class name as a function call: p = Interval(3,7)
167.32

classdef Interval<handle %AnIntervalhasaleftendandarightend properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanIntervalobj Inter.left=lt; Inter.right=rt; end

left 3 right 7 Interval() scale() shift() overlap() add()


Theconstructormethod Anobjectisalsocalled aninstanceofaclass.It containseveryproperty,instancevariable,and everyinstancemethoddefined intheclass.

left 3 right 7 Interval() scale()

... end end

Theconstructor isaspecialmethodwhose function scale(self,f) mainjobsare to % Scaletheintervalbyafactorf computethe handle of thenewobject, w= self.right self.left; self.left w*f; executetheself.right= function code+ (to assign end valuesto properties), and end returnend thehandleoftheobject. Constructorhasthenameoftheclass.

CS1112Lecture22

4/10/2013

Executing an instance method


r = Interval(4,6); r.scale(5) disp(r.right) %What will it be?
Functionspace ofscale

classdef Interval<handle %AnIntervalhasaleftendandarigh properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanInte Inter.left=lt; Inter.right=rt; end function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end end end

Object is passed to a function by reference


r = Interval(4,6); r.scale(5) disp(r.right) % updated value
Functionspace ofscale

classdef Interval<handle %AnIntervalhasaleftendandarigh properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanInte Inter.left=lt; Inter.right=rt; end

177.54

self

177.54

177.54

self

177.54

177.54

f 5 w2

177.54

f 5 w2

left 4 right 6 Interval() scale()

left 4 right 14 Interval() scale()

function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end Objectsarepassedtofunctions by reference.Changes toan end objectspropertyvaluesmadethrough thelocalreference(self) end staysintheobjectevenafterthelocalreferenceisdeletedwhen thefunction ends.

Non-objects are passed to a function by value


CommandWindowworkspace Functionspace ofscale2

Objects are passed to a function by reference


r = Interval(4,6); r.scale(5) disp(r.right) % updated value
classdef Interval<handle : methods : function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end end end

v 2

v 10 20 1 f 5

v= [2 4 1]; scale2(v,5) disp(v) %NO CHANGE

function scale2(v,f) % Scale v by a factor f v= v*f;

v= [2 4 1]; scale2(v,5) disp(v) %NO CHANGE

function scale2(v,f) % Scale v by a factor f v= v*f;

Non-objects are passed to a function by value

Syntax for calling an instance method: <reference>.<method>(<arguments for2nd thrulastparameters>)


p = Interval(3,7); r = Interval(4,6); yesno= p.isIn(r); % Explicitly call % ps isIn method yesno= isIn(p,r); % Matlab chooses the % isIn method of one % of the parameters.
classdef Interval<handle : methods : function scale(self,f) %Scaleselfbyafactorf w=self.right self.left; self.right=self.left +w*f; end function tf =isIn(self,other) %tf istrueifselfisinotherinterval tf=self.left>=other.left &&... self.right<=other.right; end

Method to find overlap between two Intervals


function Inter = overlap(self, other) % Inter is overlapped Interval between self % and the other Interval. If no overlap then % self is empty Interval.

end end

CS1112Lecture22

4/10/2013

The overlaps left (OLeft) is the rightmost of the two original lefts The overlaps right (ORight) is the leftmost of the two original rights No overlap if OLeft > ORight
Lecture22 21

function Inter = overlap(self, other) % Inter is overlapped Interval between self % and the other Interval. If no overlap then % self is empty Interval. Inter= Interval.empty(); left= max(self.left, other.left); right= min(self.right, other.right); if right-left > 0 Inter= Interval(left, right); end % Example use of overlap function end A= Interval(3,7);
Builtin function

isempty
April5,2007

April5,2007

B= Interval(4,4+rand*5); X= A.overlap(B); if ~isempty(X) fprintf((%f,%f)\n, X.left,X.right) end Lecture22 22

Overloading built-in functions You can change the behavior of a built-in function for an object of a class by implementing a function of the same name in the class definition Called overloading A typical built-in function to overload is disp
Specify which properties to display, and how, when the argument to disp is an object Matlab calls disp when theres no semi-colon at the end of an assignment statement
SeeInterval.m

classdef syntax summary


Aclassfilebeginswith keywordclassdef: classdef classname <handle
Theclassspecifies handle objects Properties

classdef Interval<handle %AnIntervalhasaleftendandarightend properties left right end methods function Inter=Interval(lt,rt) %Constructor:constructanIntervalobj Inter.left=lt; Inter.right=rt; end function scale(self,f) %Scaletheintervalbyafactorf w=self.right self.left; self.right=self.left +w*f; end end end

Constructorreturnsa referencetotheclassobject

Constructor

Eachinstancemethodsfirst parametermustbeareference Instance methods totheinstance (object)itself Usekeywordendforclassdef, properties, methods, function.

(functions)

You might also like