Docsity

Computing for Engineers - MATLAB Classes and Objects - Lecture Notes | CS 1371, Papers of Computer Science

Partial preview of the text

Download Computing for Engineers - MATLAB Classes and Objects - Lecture Notes | CS 1371 and more Papers Computer Science in PDF only on Docsity!

1

CS

Introduction to Computing

for Engineers Matlab Classes and Objects

The Java Language

2

Making Matlab Classes •^ Basic Contents of a Class •^ Deriving child classes

Learning Objectives Building your ownclasses

Object-Oriented Programs

•^ OO programming provides a way to create and useAbstract Data Types (ADT’s). •^ OBJECT: a software construct that encapsulatesbehaviors and data •^ CLASS: this is the set of plans from which an objectcan be constructed. A class is the program thatdescribes how to build an object, what kinds of data itwill contain, and what kinds of things can be done toand with this data. 4

Many OBJECTS canbe constructed andeach Instancecontains its own set ofdata and methods.

The CLASS provides the plans forthe objects to be constructed.

OBJECTS are constructed fromthe plans for the CLASS.

The Base Class BankAccount

5

BankAccount

balance

deposit withdraw

setBalance getBalance

Structure of a Matlab Class (cont)

•^ The following methods are required for all classes to bestored in its main directory:^ –^ The Constructor – 7

a method named the same as the

class [(Fred) in our example] that consumesinitialization data and produces the initial datastructure – display.m

– the method called whenever Matlab

wants to display the contents of an object – char.m^ – a method returning a character stringdescribing this object • Any other methods required to manipulate the data forthis object are also stored here. – set/get methods to access each data item [actually,for child classes, but a good general principle]

@BankAccount\BankAccount.m

function acct = BankAccount(data)% BankAccount class constructor.%^ ba = BankAccount(amt) creates a bank account%^ 8

with balance amt

% could also be a copy contructor:%^ ba = BankAccount( oba ) copies the account obaif nargin == 0acct.balance = 0;acct = class(acct,'BankAccount');elseif isa(data, 'BankAccount')acct = data;elseacct.balance = data;acct = class(acct,'BankAccount');end

Name of the class (matchesthe file and directory name)^ Must provide for being called asjust BankAccount^ local data is a structurelocal data is a structure^ Change the type of the local^ Change the type of the localdata to the class namedata to the class name^ If the parameter is aBankAccount, just copy it

@BankAccount\deposit.m && withdraw.m^ function deposit(acct, amount)% deposit to the accountsetBalance(acct, acct.balance + amount);assignin('caller', inputname(1), acct); 10

Ugly syntax that writesthe new account back tothe calling program

function gets = withdraw(acct, amount)% withdraw from the accountgets = amount;if gets > acct.balance

gets = acct.balance;endsetBalance(acct, acct.balance - gets);assignin('caller', inputname(1), acct);

Ugly syntax that writesthe new account back tothe calling program Call local method to set Call local method to setthe balancethe balance

@BankAccount \ accessing the data function ans = getBalance(ba)% get the balanceans = ba.balance; function setBalance(acct, amount)% set the account balanceacct.balance = amount;assignin('caller', inputname(1), acct); 11

Ugly syntax that writesthe new account back tothe calling program

13

Questions?

Illustration of Inheritance by Extension 14

BankAccount

balance

getBalance deposit withdraw

setBalance

RATEMIN_BALANCE

SavingsAccount calcInterest

Stored in @BankAccount Stored in @SavingsAccount

Coding Derived Classes - methods function^ int^ = 16

calcInterest(acct) %^ calculate

the^ interest if^ getBalance(acct)

^ acct.MIN_BALANCE int =^ acct.RATE

*^ getBalance(acct)

/^ 100;

elseint =^

end

Using the Savings Account class

sa^ =^ 17 SavingsAccount(2000)deposit(sa,^ 3000);fprintf('deposit

3000 ->^ %s\n',

char(sa)^

intrst^ =^ calcInterest(sa);deposit(sa,

intrst); fprintf('deposit

interest^

%8.2f^ ->^ %s\n',

intrst,^ char(sa)

Coding the Delux Savings Class

function 19 acct^ =^ DeluxSavingsAccount(data)% DeluxSavingsAccount

class^ constructor. %^ ba^ =^ DeluxSavingsAccount(amt)

creates^ a

bank^ account

with^ balance

amt %^ could^ also

be^ a^ copy

constructor: %^ ba^ =^ DeluxSavingsAccount(

oba^ )^ copies

the^ account

if^ nargin^

==^0

parent =^ SavingsAccount;acct.overdraftOK =

false; acct.OVERDRAFT_CHARGE

=^ 20;

acct^ =^ class(acct,'DeluxSavingsAccount',

parent^ );

elseif^ isa(data,'DeluxSavingsAccount')acct^ =^

data; elseparent^

=^ SavingsAccount(data);acct.overdraftOK =

false; acct.OVERDRAFT_CHARGE

=^ 20;

acct^ =^ class(acct,'DeluxSavingsAccount',

parent^ );

end

Coding Derived Classes - methods

20

function^ allowOverdraft(acct, value)% approve overdraft on

DeluxSavingsAccount acct.overdraftOK = value;assignin('caller', inputname(1), acct);

function^ gets = withdraw(acct, amount)%^ withdraw^

from the accountif (amount < getBalance(acct)) || ~acct.overdraftOKparent = acct.SavingsAccount;gets = withdraw(parent, amount);acct.SavingsAccount = parent;elsegets = amount;penalty = 0;if gets > getBalance(acct)penalty = acct.OVERDRAFT_CHARGE;endsetBalance(acct, getBalance(acct) - gets - penalty);endassignin('caller', inputname(1)

, acct);

Use the parent’s withdrawmethod^ Every child class has adata item with the nameof the parent class

Always access parents’ datavia access methods – can’tuse .balance directly!

Quiz time: why can’t we just say: gets = withdraw(acct.SavingsAccount,

amount);