1 / 21

OOP in VB

OOP in VB. OOP Principles. An objec t – in memory – has Some data values members Pieces of executable code methods Objects usually come and go during execution – dynamic not static Objects belong to a class – the type of the object An object is an instantiation of the class.

Download Presentation

OOP in VB

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. OOP in VB

  2. OOP Principles • An object – in memory – has • Some data values members • Pieces of executable code methods • Objects usually come and go during execution – dynamic not static • Objects belong to a class – the type of the object • An object is an instantiation of the class

  3. OOP Principles- Encapsulation • Contrast with structured programming, where global data is processed by functions • Encapsulation = enclosing the data in an object • Other code (in other classes) cannot change the data in an object • No global data in pure OOP • High level of modularity • Data members should be private • Accessor methods of the class control access • Changes are (should be) validated

  4. Typical classes (objects are nouns) • Application-specific classes – • employee, invoice, contract, purchase order • dust particle in graphics for simulation • System classes • UI – window, form, button, slider • System – thread, file, socket

  5. Inheritance • A class can be designed as a variation on another class (the base or ancestor class) • The subclass inherits all the members and methods of the base class • Supports code re-use – do not have to start from scratch

  6. Polymorphism • Sub-classes can have new or different data members of methods • So can have variations on a theme • eg a button is a sub-class of a window

  7. Object lifetime • An object is 'made' at run-time • Using the keyword new • Good OOP languages have constructors which make new objects • and destructors to end objects and free up memory

  8. References • In VB (and Java) classes are reference types • References are (hidden) pointers • No (exposed) pointers in VB or Java • In VB use Set eg- Dim myObject as myClass ' say what type it is Set myObject = new myClass ' actually make one Dim object2 as myClass ' another reference Set object2 = myObject ' to the same object

  9. Garbage collection • GC is when memory manager reclaims memory occupied by objects no longer needed – reused as free memory • In C++ can explicitly destroy objects – memory reclaimed then • In VB – system counts the number of references – when =0, GC happens • Only important for large arrays of large objects

  10. Classes in VB • Class defined in a class module (name = name of class) • Inside the class module you • declare private data members • methods as public or private sub or functions • Property Get or Let to access private data members

  11. Add Class module to project • In Project.. Add class module • Change name..

  12. Example – particle object • Idea – simulate physical system containing many particles • Each particle needs data members – position and velocity • And methods – initialise, move, and draw • An array of these objects represents a bunch of particles

  13. Look at the OOPParticle project • Run it • Look through the code • Try changing the number of particles, the viscous drag and so on. • Develop it – • All the particles are green • Add data members for their red green and blue colour components • Initialise them as random • Change the draw method to use the colour

  14. Interfaces • In VB, an interface is a single way of 'talking to' different classes • Interface written just like a class, but with empty methods • different classes can implement methods in different ways • gives polymorphism Class 1 Method A (version 1) Method B Method C Calling code – Class1.methodA Class2.methodA Class3.methodA Interface Method A Method B Method C Class 2 Method A (version 2) Method B Method C Class 3 Method A (version 3) Method B Method C

  15. Example interface - personnel • Dealing with Employees • All have name, rateOfPay properties • All need a pay method • Managerial employees are monthly paid • Clerical employees are hourly paid

  16. The Employee interface Private nameStr As String Private payRate As Double Public Property Let name(val As String) End Property Public Property Get name() As String End Property Public Property Get rateOfPay() As Double End Property Public Property Let rateOfPay(val As Double) End Property Public Sub pay() End Sub

  17. The Manager Class Implements Employee Private nameStr As String Private payRate As Double Public Property Get Employee_name() As String Employee_name = nameStr End Property Public Property Let Employee_name(val As String) nameStr = val End Property Public Property Get Employee_rateOfPay() As Double Employee_rateOfPay = payRate End Property Public Property Let Employee_rateOfPay(val As Double) payRate = val End Property Public Sub Employee_pay() MsgBox (nameStr & " gets paid " & payRate) End Sub

  18. Testing the Manager class Dim emp1 As Manager Set emp1 = New Manager emp1.Employee_name = "John (manager)" emp1.Employee_rateOfPay = 2010 emp1.Employee_pay

  19. The Clerical Class (interface example) Implements Employee Private nameStr As String Private payRate As Double ' extra data member Private hours As Integer 'extra properties Public Property Let hoursWorked(val As Integer) hours = val End Property Public Property Get hoursWorked() As Integer hoursWorked = hours End Property ..rest same as Manager class except.. Public Sub Employee_pay() MsgBox (nameStr & " gets paid " & payRate * hours) End Sub

  20. Using the Clerical Class Dim emp2 As Clerical Set emp2 = New Clerical emp2.Employee_name = "Luke (clerical)" emp2.Employee_rateOfPay = 8.5 emp2.hoursWorked = 100 emp2.Employee_pay

  21. Interfaces are not class hierarchies • There is no inheritance in VB6 • There are no derived classes in VB • In the above, Clerical is not a sub-class of Employee • Members must be re-coded

More Related