Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a...
-
Upload
logan-jewel -
Category
Documents
-
view
218 -
download
2
Transcript of Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a...
![Page 1: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/1.jpg)
CSM18 VB OOP 1 Dr Terry Hinton 28/10/03
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
![Page 2: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/2.jpg)
CSM18 VB OOP 2 Dr Terry Hinton 28/10/03
Object Oriented Programming
Classes – templates for objects The “cookie cutter” idea
Class Definition Objects
![Page 3: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/3.jpg)
CSM18 VB OOP 3 Dr Terry Hinton 28/10/03
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
![Page 4: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/4.jpg)
CSM18 VB OOP 4 Dr Terry Hinton 28/10/03
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
![Page 5: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/5.jpg)
CSM18 VB OOP 5 Dr Terry Hinton 28/10/03
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
WithdrawCash
DepositCash
User (Actor)
![Page 6: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/6.jpg)
CSM18 VB OOP 6 Dr Terry Hinton 28/10/03
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
![Page 7: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/7.jpg)
CSM18 VB OOP 7 Dr Terry Hinton 28/10/03
Class Model based on Use-Case Diagram
Having identified classes, need to define… Interactions between
them Structure Class Interfaces - public
view of object
ATMObj
ValidateUserPrintStatementMakeDepositMakeWithdrawal
AccountObj
BalancePINOwner
DepositWithdrawStatement
ATM AccountAccount.Deposit(50)
![Page 8: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/8.jpg)
CSM18 VB OOP 8 Dr Terry Hinton 28/10/03
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
![Page 9: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/9.jpg)
CSM18 VB OOP 9 Dr Terry Hinton 28/10/03
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
![Page 10: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/10.jpg)
CSM18 VB OOP 10 Dr Terry Hinton 28/10/03
Account Class
Private Balance As Currency ‘ very simple examplePrivate PIN As IntegerPrivate 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
Private Balance As Currency ‘ very simple examplePrivate PIN As IntegerPrivate 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
![Page 11: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/11.jpg)
CSM18 VB OOP 11 Dr Terry Hinton 28/10/03
Account – Methods
Public Function GetBalance() As CurrencyGetBalance = Balance
End Function
Public Funtion GetPIN() As IntegerGetPin = PIN
End Function
Public Sub AssignPIN(newValue As Integer)PIN = newValue
End Sub
Public Sub Deposit(amount As Currency)Balance = Balance + amount
End Sub
Public Function GetBalance() As CurrencyGetBalance = Balance
End Function
Public Funtion GetPIN() As IntegerGetPin = 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
ReadWrite
Method definition
![Page 12: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/12.jpg)
CSM18 VB OOP 12 Dr Terry Hinton 28/10/03
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
![Page 13: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/13.jpg)
CSM18 VB OOP 13 Dr Terry Hinton 28/10/03
Testing Account
Set Account = New AccountObjAccount.AssignOwner ( “John Smith”)Account.AssignPIN (1234)Account.Deposit (500.00)Account.StatementStatement for: John Smith Balance = £500.00Account .Withdraw (50)Account.StatementStatement for: John Smith Balance = £450.00‘ etc…
Set Account = New AccountObjAccount.AssignOwner ( “John Smith”)Account.AssignPIN (1234)Account.Deposit (500.00)Account.StatementStatement for: John Smith Balance = £500.00Account .Withdraw (50)Account.StatementStatement 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.
![Page 14: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/14.jpg)
CSM18 VB OOP 14 Dr Terry Hinton 28/10/03
Continue Development
Create next class ATM User-Interactions with ATM should
translate into ATM Methods and interactions with Account Methods
![Page 15: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/15.jpg)
CSM18 VB OOP 15 Dr Terry Hinton 28/10/03
Example ATM Method
Public Function ValidateUser(Account As AccountObj) As BooleanDim userPIN As Integer
userPIN = InputBox(“Enter PIN”)If TestAccount.PIN = userPIN Then
ValidateUser = TrueElse
ValidateUser = FalseEnd If
End Function
Public Function ValidateUser(Account As AccountObj) As BooleanDim userPIN As Integer
userPIN = InputBox(“Enter PIN”)If TestAccount.PIN = userPIN Then
ValidateUser = TrueElse
ValidateUser = FalseEnd If
End Function This method takes as its parameter an Object of type AccountObj. Returns a Boolean.
![Page 16: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/16.jpg)
CSM18 VB OOP 16 Dr Terry Hinton 28/10/03
Testing an ATM Method
Set Account = New AccountObjAccount.AssignOwner (“John Smith”)Account.AssignPIN = 1234Set ATM = New ATMObjPrint 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 SmithBalance = £100.00
Set Account = New AccountObjAccount.AssignOwner (“John Smith”)Account.AssignPIN = 1234Set ATM = New ATMObjPrint 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 SmithBalance = £100.00
![Page 17: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/17.jpg)
CSM18 VB OOP 17 Dr Terry Hinton 28/10/03
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.
![Page 18: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/18.jpg)
CSM18 VB OOP 18 Dr Terry Hinton 28/10/03
References and Objects
Dim Account As AccountObj, TempAccount As AccountObj
Set Account = New AccountObj
Set TempAccount = Account
Dim Account As AccountObj, TempAccount As AccountObj
Set Account = New AccountObj
Set TempAccount = AccountCopies reference for
Account to TempAccount
Object: Account
Creates two references of type AccountObjCreates object instance and sets aside space for variables
Only one instance of Account
![Page 19: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/19.jpg)
CSM18 VB OOP 19 Dr Terry Hinton 28/10/03
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
![Page 20: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/20.jpg)
CSM18 VB OOP 20 Dr Terry Hinton 28/10/03
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
![Page 21: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/21.jpg)
CSM18 VB OOP 21 Dr Terry Hinton 28/10/03
A Collection
Set Queue = New CollectionObjSet Account = New AccountObjAccount.AssignOwner (“Fred”)Queue.Add (Account)Set Account = New AccountObjAccount.AssignOwner (“Mary”)Queue.Add (Account)Print Queue.Count2Print Queue(1).OwnerFredPrint Queue(2).OwnerMary
Set Queue = New CollectionObjSet Account = New AccountObjAccount.AssignOwner (“Fred”)Queue.Add (Account)Set Account = New AccountObjAccount.AssignOwner (“Mary”)Queue.Add (Account)Print Queue.Count2Print Queue(1).OwnerFredPrint Queue(2).OwnerMary
![Page 22: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/22.jpg)
CSM18 VB OOP 22 Dr Terry Hinton 28/10/03
Collections and For Each…
Collections and objects introduce a new style of For..Next loop
For Each Account In Queue Print Account.OwnerNext
For Each Account In Queue Print Account.OwnerNext
FredMaryFredMary
![Page 23: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/23.jpg)
CSM18 VB OOP 23 Dr Terry Hinton 28/10/03
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
![Page 24: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/24.jpg)
CSM18 VB OOP 24 Dr Terry Hinton 28/10/03
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
![Page 25: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/25.jpg)
CSM18 VB OOP 25 Dr Terry Hinton 28/10/03
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
![Page 26: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/26.jpg)
CSM18 VB OOP 26 Dr Terry Hinton 28/10/03
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
![Page 27: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/27.jpg)
CSM18 VB OOP 27 Dr Terry Hinton 28/10/03
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
ItemAdded
ItemAdded
ItemAdded
ItemRemoved
ItemRemoved
ItemRemoved
ItemAdded
![Page 28: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/28.jpg)
CSM18 VB OOP 28 Dr Terry Hinton 28/10/03
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’sfather
My father’sMother
My mother’s My mother’smotherfather
![Page 29: Dr Terry Hinton 28/10/03 CSM18 VB OOP1 Visual Basic: An Object Oriented Approach Programming is a Model of the Real World Object in Real World modelled.](https://reader036.fdocuments.in/reader036/viewer/2022070307/551ae8185503466b6a8b5f38/html5/thumbnails/29.jpg)
CSM18 VB OOP 29 Dr Terry Hinton 28/10/03
Graphs
Arbitrary interconnections Complex, and can be inefficient to code Require use of specialist algorithms to search and traverse
NodeA
NodeB
NodeE
NodeF
NodeD
NodeC