1 / 29

Visual Basic: An Object Oriented Approach

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. Object Oriented Programming. Classes – templates for objects The “cookie cutter” idea. Class Definition. Objects.

shanna
Download Presentation

Visual Basic: An Object Oriented Approach

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 1

  2. Object Oriented Programming • Classes – templates for objects • The “cookie cutter” idea Class Definition Objects 2

  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 3

  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 4

  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 User (Actor) Deposit Cash 5

  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 6

  7. Account.Deposit(50) ATM Account 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 7

  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 8

  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 9

  10. Account Class Methods are Subroutines & Functions 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 10

  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 11

  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 12

  13. Testing Account • 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. 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… 13

  14. Continue Development • Create next class ATM • User-Interactions with ATM should translate into ATM Methods and interactions with Account Methods 14

  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. 15

  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 16

  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. 17

  18. References and Objects Dim Account As AccountObj, TempAccount As AccountObj Set Account = New AccountObj Set TempAccount = Account Creates two references of type AccountObj Creates object instance and sets aside space for variables Object: Account Copies reference for Account to TempAccount Only one instance of Account 18

  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 19

  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 20

  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 21

  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 22

  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 23

  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 24

  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 25

  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 26

  27. Queues and Stacks Collections with limitations on insertions/retrievals Rear of Queue (items join here) Front of Queue (items leave here) Item 5 Item 4 Item 3 Item 2 Item 1 27

  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 My father’s My mother’s My mother’s father Mother mother father 28

  29. Graphs • Arbitrary interconnections • Complex, and can be inefficient to code • Require use of specialist algorithms to search and traverse Node Node A B Node Node C D Node Node E F 29

More Related