Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 as an object in the program Hence Object Oriented Programming

2 Dr Terry Hinton 28/10/03 CSM18 VB OOP2 Object Oriented Programming Classes – templates for objects The “cookie cutter” idea Class Definition Objects

3 Dr Terry Hinton 28/10/03 CSM18 VB OOP3 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

4 Dr Terry Hinton 28/10/03 CSM18 VB OOP4 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

5 Dr Terry Hinton 28/10/03 CSM18 VB OOP5 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)

6 Dr Terry Hinton 28/10/03 CSM18 VB OOP6 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

7 Dr Terry Hinton 28/10/03 CSM18 VB OOP7 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 ATMAccount Account.Deposit(50)

8 Dr Terry Hinton 28/10/03 CSM18 VB OOP8 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

9 Dr Terry Hinton 28/10/03 CSM18 VB OOP9 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

10 Dr Terry Hinton 28/10/03 CSM18 VB OOP10 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 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

11 Dr Terry Hinton 28/10/03 CSM18 VB OOP11 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 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

12 Dr Terry Hinton 28/10/03 CSM18 VB OOP12 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

13 Dr Terry Hinton 28/10/03 CSM18 VB OOP13 Testing Account Set Account = New AccountObj Account.AssignOwner ( “John Smith”) Account.AssignPIN (1234) Account.Deposit (500.00) Account.Statement Statement for: John Smith Balance = £ Account.Withdraw (50) Account.Statement Statement for: John Smith Balance = £ ‘ etc… Set Account = New AccountObj Account.AssignOwner ( “John Smith”) Account.AssignPIN (1234) Account.Deposit (500.00) Account.Statement Statement for: John Smith Balance = £ Account.Withdraw (50) Account.Statement Statement for: John Smith Balance = £ ‘ 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.

14 Dr Terry Hinton 28/10/03 CSM18 VB OOP14 Continue Development Create next class ATM User-Interactions with ATM should translate into ATM Methods and interactions with Account Methods

15 Dr Terry Hinton 28/10/03 CSM18 VB OOP15 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 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.

16 Dr Terry Hinton 28/10/03 CSM18 VB OOP16 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 = £ 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

17 Dr Terry Hinton 28/10/03 CSM18 VB OOP17 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.

18 Dr Terry Hinton 28/10/03 CSM18 VB OOP18 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 = 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

19 Dr Terry Hinton 28/10/03 CSM18 VB OOP19 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

20 Dr Terry Hinton 28/10/03 CSM18 VB OOP20 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

21 Dr Terry Hinton 28/10/03 CSM18 VB OOP21 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 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

22 Dr Terry Hinton 28/10/03 CSM18 VB OOP22 Collections and For Each… Collections and objects introduce a new style of For..Next loop For Each Account In Queue Print Account.Owner Next For Each Account In Queue Print Account.Owner Next Fred Mary Fred Mary

23 Dr Terry Hinton 28/10/03 CSM18 VB OOP23 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

24 Dr Terry Hinton 28/10/03 CSM18 VB OOP24 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

25 Dr Terry Hinton 28/10/03 CSM18 VB OOP25 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

26 Dr Terry Hinton 28/10/03 CSM18 VB OOP26 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

27 Dr Terry Hinton 28/10/03 CSM18 VB OOP27 Queues and Stacks Collections with limitations on insertions/retrievals Item 5Item 4Item 3Item 2Item 1 Front of Queue (items leave here) Rear of Queue (items join here)

28 Dr Terry Hinton 28/10/03 CSM18 VB OOP28 Alternatives to Collections Tree Structures Each item can contain references to several other items Efficient for storage and retrieval Recursive Me My FatherMy Mother My father’s father My father’s Mother My mother’s mother father

29 Dr Terry Hinton 28/10/03 CSM18 VB OOP29 Graphs Arbitrary interconnections Complex, and can be inefficient to code Require use of specialist algorithms to search and traverse Node A B E F D C


Download ppt "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."

Similar presentations


Ads by Google