CSM18-VB OOP

processroguishSoftware and s/w Development

Nov 18, 2013 (3 years and 6 months ago)

74 views

Dr Terry Hinton 28/10/03

CSM18 VB OOP

1

Visual Basic:

An Object Oriented Approach


Programming is a Model of the Real World


Object in Real World modelled as an object in
the program


Hence Object Oriented Programming

Dr Terry Hinton 28/10/03

CSM18 VB OOP

2

Object Oriented Programming


Classes


templates for objects


The “cookie cutter” idea

Class Definition

Objects

Dr Terry Hinton 28/10/03

CSM18 VB OOP

3

Object Oriented Programming


Classes


templates for objects


Encapsulation


of Data and Code


Attributes of an object
-

variables


Methods


operations defined for a class of
objects


Messages


interactions between objects
via Methods to send or receive data

Dr Terry Hinton 28/10/03

CSM18 VB OOP

4

Object Models


Real programs may involve large numbers of
objects


Will interact in various ways


Will form logical structures


Key is in building an object model that provides a
‘world view’ of the system


One to one correspondence between object in the
real world and object in the model

Dr Terry Hinton 28/10/03

CSM18 VB OOP

5

Use
-
Case Diagrams


Used to obtain a high
level (user
-
level) picture
of the system


Can be used in
discussions with clients


Shows main interactions
between system and
users/external
environment

ATM
-

Bank Machine

Accept Card

Verify PIN

Print

Statement

Withdraw

Cash

Deposit

Cash

User
(Actor)

Dr Terry Hinton 28/10/03

CSM18 VB OOP

6

Class Model Diagrams

Class Name






Attributes

-

Variables define state of object


Methods

-

Program code defines processes or behaviour of object




methods interact with methods of other objects

Dr Terry Hinton 28/10/03

CSM18 VB OOP

7

Class Model based on Use
-
Case
Diagram


Having identified classes,
need to define…


Interactions between them


Structure


Class Interfaces
-

public
view of object


ATMObj



ValidateUser

PrintStatement

MakeDeposit

MakeWithdrawal

AccountObj


Balance

PIN

Owner


Deposit

Withdraw

Statement

ATM

Account

Account.Deposit(50)

Dr Terry Hinton 28/10/03

CSM18 VB OOP

8

Coding a Class


Need to…


Determine storage needs
-

attributes or variables


Determine Methods and interaction between them


Consider mechanisms for getting data into and
out of an object


Define Methods & Functions to do this


Data
-

Private


Methods & Functions
-

Public


Dr Terry Hinton 28/10/03

CSM18 VB OOP

9

Example


Bank/ATM


Assume two classes only


ATM Class models Automatic Teller Machine


Account class models individual account in Bank


Start with Account class


ATM accesses Account


Account receives deposit, agree a withdrawal, present state
of balance, agree PIN number

Dr Terry Hinton 28/10/03

CSM18 VB OOP

10

Account Class

Private Balance As Currency ‘ very simple example

Private PIN As Integer

Private Owner As String


Public Function GetBalance() As Currency…

Public Function GetPIN() As Integer…

Public Sub AssignPIN(newValue As Integer)…

Public Function GetOwner() As String…

Public Sub AssignOwner(newValue As String)…

Public Sub Deposit(amount As Currency)…

Public Sub Withdraw(amount As Currency)…

Public Function Statement() As String…
‘returns text about account

Methods are Subroutines & Functions

Dr Terry Hinton 28/10/03

CSM18 VB OOP

11

Account


Methods

Public Function GetBalance() As Currency


GetBalance = Balance

End Function


Public Funtion GetPIN() As Integer


GetPin = PIN

End Function


Public Sub AssignPIN(newValue As Integer)


PIN = newValue

End Sub


Public Sub Deposit(amount As Currency)


Balance = Balance + amount

End Sub

Read
-
only

Read
-
only
property

Read

Write

Method
definition

Dr Terry Hinton 28/10/03

CSM18 VB OOP

12

Class
-

Object
-

Instances


Define a Class of Objects but need an actual
example of an object


Instance of a Class
-

Object Instance


Each Object has the same set of attributes


Each Object has its own values of these attributes


Values of Attributes set the state of the Object


Only one copy of an Object Instance but have
any number of Place Holders for a reference or
pointer to an Object
-

more later

Dr Terry Hinton 28/10/03

CSM18 VB OOP

13

Testing Account

Set Account = New AccountObj

Account.AssignOwner ( “John Smith”)

Account.AssignPIN (1234)

Account.Deposit (500.00)

Account.Statement

Statement for: John Smith

Balance = £500.00

Account .Withdraw (50)

Account.Statement

Statement for: John Smith

Balance = £450.00

‘ etc…


In VB can use the Immediate
Window to test a class


Create an object


Execute methods


Print attribute values (Using
Print)


Can also copy, the sequence of
test statements, paste into
Notepad or an editor, and save
for re
-
testing if changes are
made.


Dr Terry Hinton 28/10/03

CSM18 VB OOP

14

Continue Development


Create next class ATM


User
-
Interactions with ATM should translate
into ATM Methods and interactions with
Account Methods

Dr Terry Hinton 28/10/03

CSM18 VB OOP

15

Example ATM Method

Public Function ValidateUser(Account As AccountObj) As Boolean

Dim userPIN As Integer


userPIN = InputBox(“Enter PIN”)


If TestAccount.PIN = userPIN Then



ValidateUser = True


Else



ValidateUser = False


End If

End Function

This method takes as its

parameter an Object of
type AccountObj. Returns a
Boolean.

Dr Terry Hinton 28/10/03

CSM18 VB OOP

16

Testing an ATM Method

Set Account = New AccountObj

Account.AssignOwner (“John Smith”)

Account.AssignPIN = 1234

Set ATM = New ATMObj

Print ATM.ValidateUser (Account)

‘ VB creates an InputBox() here to enter PIN number

‘ ATM returns True or False depending on input to it.

‘ Could now have ATM making deposits and

‘ withdrawals etc. e.g…

ATM.MakeDeposit (100.00)

ATM.PrintStatement (Account)

Statement for: John Smith

Balance = £100.00

Dr Terry Hinton 28/10/03

CSM18 VB OOP

17

References


A Reference is a variable that acts as an alias for
an object or pointer to an object


References are used for interacting with objects
avoids copying objects


An object with no references to it is destroyed
automatically
-

orphan


When an object instance is created it is given a
name and allocated space for its attributes


Reference to that object can be made without
copying the Object.

Dr Terry Hinton 28/10/03

CSM18 VB OOP

18

References and Objects

Dim Account As AccountObj,


TempAccount As AccountObj


Set Account = New AccountObj



Set TempAccount = Account

Copies reference for

Account to

TempAccount

Object:


Account

Creates two references
of type AccountObj

Creates object instance
and sets aside space
for variables

Only one instance of Account

Dr Terry Hinton 28/10/03

CSM18 VB OOP

19

Collections


When we create a set of object instances
they all have the same name!!


To avoid losing track of Objects we need a
Collection which is a list of object
references


Therefore no need to give each object instance
a different name


Acts as an unbounded array


no need to
indicate how many elements to accommodate

Dr Terry Hinton 28/10/03

CSM18 VB OOP

20

Collection Methods


Four main methods


Add


adds an object reference to the collection


Remove


removes a reference


Count


returns number of references in the collection


Item


allows access to individual object references


Indexing


Objects can be retrieved by number


Objects can be retrieved by value of an attribute


Objects can be added with a Key (string), which acts
as a textual index

Dr Terry Hinton 28/10/03

CSM18 VB OOP

21

A Collection

Set Queue = New CollectionObj

Set Account = New AccountObj

Account.AssignOwner (“Fred”)

Queue.Add (Account)

Set Account = New AccountObj

Account.AssignOwner (“Mary”)

Queue.Add (Account)

Print Queue.Count

2

Print Queue(1).Owner

Fred

Print Queue(2).Owner

Mary

Dr Terry Hinton 28/10/03

CSM18 VB OOP

22

Collections and For Each…


Collections and objects introduce a new
style of For..Next loop


For Each Account In Queue


Print Account.Owner

Next


Fred

Mary

Dr Terry Hinton 28/10/03

CSM18 VB OOP

23

Building Structures with Objects


Class CarObj


Private ID As Integer, mileage As Single


Private Passenger As PassengerObj


Public Sub AcceptPass(Pass As PassengerObj)


Public Sub Journey


CurrentName = Car.GetPass.GetName

Dr Terry Hinton 28/10/03

CSM18 VB OOP

24

Structures in programs


Ways of grouping objects using


Arrays, Collections


Lists


Simple lists of objects


Stacks (Last
-
in, First out)


Queues (Last
-
in, Last
-
out)


Trees


Hierarchical structures of objects
-

node contains
reference to next node


Graphs


Arbitrarily interconnected objects

Dr Terry Hinton 28/10/03

CSM18 VB OOP

25

Logical and Physical Structure


Interconnect objects using references


Forms of interconnection involve


Logical structure


The model of interconnections we wish to create


Physical structure code


The actual mechanisms we use to create it

Dr Terry Hinton 28/10/03

CSM18 VB OOP

26

Alternative Collections


Ordered Collection


Items added in a specific order


Can use binary search to insert and retrieve items


Efficiency in searching (and therefore
retrieval

Dr Terry Hinton 28/10/03

CSM18 VB OOP

27

Queues and Stacks

Collections with limitations on insertions/retrievals


Item 5

Item 4

Item 3

Item 2

Item 1

Front of Queue

(items leave here)

Rear of Queue

(items join here)

Item 3
Item 2
Item 1
Item 2
Item 1
Item 4
Item 3
Item 2
Item 1
Item 3
Item 2
Item 1
Item 1
Item 2
Item 1
Item 1
Item
Added
Item
Added
Item
Added
Item
Removed
Item
Removed
Item
Removed
Item
Added
Dr Terry Hinton 28/10/03

CSM18 VB OOP

28

Alternatives to Collections


Tree Structures


Each item can contain references to several
other items


Efficient for storage and retrieval


Recursive

Me

My Father

My Mother

My father’s

father

My father’s

Mother

My mother’s

My mother’s

mother

father

Dr Terry Hinton 28/10/03

CSM18 VB OOP

29

Graphs


Arbitrary interconnections


Complex, and can be inefficient to code


Require use of specialist algorithms to search and
traverse

Node

A

Node

B

Node

E

Node

F

Node

D

Node

C