CS1371 Introduction to Computing for Engineers - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

CS1371 Introduction to Computing for Engineers

Description:

If have a re-grade issue, go to help desk. Deadline is two weeks after ... George is an INSTANCE of COLLIE. Object Classes define: Data (public and private) ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 22
Provided by: david3049
Category:

less

Transcript and Presenter's Notes

Title: CS1371 Introduction to Computing for Engineers


1
CS1371Introduction to Computing for Engineers
  • Classes and Objects (continued)

2
Administrivia
  • If have a re-grade issue, go to help desk.
    Deadline is two weeks after exam next Wed.
  • Still reading
  • Smith, Chapter 23,24
  • It's Friday

3
Where we are so far
  • OBJECT ORIENTED PROGRAMMING is like structs with
    behavior
  • An OBJECT is an INSTANCE of a CLASS
  • George is an INSTANCE of COLLIE
  • Object Classes define
  • Data (public and private)
  • Methods functions (public and private)
  • CONSTRUCTOR method makes the objects

4
Behavioral Abstraction
  • Behavioral Abstractions combine procedural and
    data abstractions.

5
Do Simple BankAccount example
  • ba BankAccount(100)
  • getBalance, setBalance
  • ba2 BankAccount(ba) copies
  • when just type ba Matlab calls the display
    method, which calls the char method to get a
    string and the disp function to use it.
  • For now made setBalance callable

6
OO in Matlab (so far)
  • To create a CLASS, all it's methods (including
    the constructor) must be in a directory called
    _at_NAME (where NAME is the the class NAME).
  • Each class must have some system-useful methods
  • char, display
  • Also, are the methods that do useful work
  • e.g. getBalance, Deposit
  • And then there was this assignin nonsense

7
We've told you a lie..
  • We said
  • In Matlab, all arguments are passed into
    functions by COPYING them ("call by VALUE")
  • So if you change something in the argument
    variable, that variable in the CALLING
    environment doesn't change.
  • This is true (show example)

8
Matlab Struct Example
  • Create a new function call, called AddPounds
  • function addPounds(person,newfat)
  • person.weight person.weight newfat
  • disp(sprintf('s 's wieght is now f/n'
    person.name, person.weight)
  • p1.name 'George Thomas'
  • p1.weight 120
  • addPounds(p1,10)
  • p1.weight is still 120
  • Look at setBalance (not private for now) return
    the new balance, in case account is frozen
  • Comment out in assignin

9
But
  • But we also said
  • Since you can't change the value of the passed in
    variable that will take effect outside the
    function, we must pass back the new value for
    assignment.
  • Example again

10
Modify setBalance
  • Modify to return both the account and the new
    balance
  • Look at getbalance should it return the account
    also???
  • Gets ugly

11
But
  • But we also said
  • Since you can't change the value of the passed in
    variable that will take effect outside the
    function, we must pass back the new value for
    assignment.
  • That was a lie
  • You can assign a value to a variable where that
    ASSIGNment is IN some particular environment.
  • assignin(environment,name, value)
  • Used whenever you change the object

12
assignin
  • add back into setBalance
  • inputname(1) is the first argument

13
Public Versus Private
  • Data is NOT reachable from the outside unless you
    provide a method to get it.
  • Can't do acct.balance
  • So we defined getBalance
  • Methods ARE reachable (callable) unless they are
    in the 'PRIVATE' subdirectory of the clas
    directory.
  • Move setBalance

14
Private data and methods
  • move setBalance to the PRIVATE directory

15
Having children
  • The real power of OOP is the ability to have
    children classes.
  • Children classes INHERIT all the abstractions
    (data and methods) of their parents, but can also
    have specialized ones. (Example in a minute.)
  • Children have following properties
  • All children can be treated as a instance of
    their own class, or any of their ancestor classes
    (parent, parent's parent, etc)
  • But, will always get the "right" method for them.

Polymorhpism
16
Children Classes Savings Account
  • Consider Savings account A BankAccount with
    some more data and methods.
  • New data
  • interest rate
  • Min Balance
  • New methods
  • calculate interest
  • set, get the rate (??)

17
Look at Methods for Savings code
  • Look at system ones
  • constructor adds interest rate and min_balance
  • But more importantly look at
  • acct class(acct, 'SavingsAccount', super)
  • char gives new string - we'll look at in a
    minute
  • Why is there no display method????
  • Because all savings accounts are also
    BankAccounts, so they have the display method of
    BankAccount.

18
Can Call Parent's Method Directly
  • Sometimes, you think what your parent wants to do
    is completely wrong for you (later)
  • But sometimes, they are almost right, and you
    just want to take what they do and change it a
    little.
  • Eample the char method for SavingsAccount

19
Char Method for Savings Account
  • ba bankaccount(100)
  • call char method ba.
  • sa SavingsAccount(100)
  • Then call char method on sa.
  • Look at sa.BankAccount in the char method

20
Adding a variable
  • To initialize with many values, pass a struct
  • Then need to have set and get functions

21
Do for SSNUM
  • Do it in BankAccount
Write a Comment
User Comments (0)
About PowerShow.com