Visual Basic Explorer
Visual Basic Explorer
 Navigation
 Home


 Coding
 Source Code

 FAQ Center

 VB Tips

 Downloads

 ToolBox

 Tutorials

 VB Games

 VB News

 VB Award

 VB Forums



 Affiliates
 Planet Source Code

 Rent a Coder

 DirectX4VB


 Misc
 Search

 Feedback

 Advertise

 About


Need to hire
a VB coder?

Please support our sponsor:

 Home 
 Site Map 
 Forums 
 News 
 Feedback 

OOP:Object Oriented Programming


Name: Understanding OOP
Author: Derrick Mead
Date: 8/18/97
Lesson: 2

Encapsulation

One of the fundamental concepts of OOP is the method with which objects store and manipulate data known as Encapsulation.

Each object consists of internal variables which, when exposed to outside world, become its' attributes. That is, the data that is input and output from the object. VB defines these exposed attributes as Properties.

In addition to knowing what data to deal with, each object knows how to manipulate that data. Functions and Procedures that perform tasks are called Methods. A Method can be used internally or exposed to the programmer. If a Method is declared as Private then it can only be used inside the object. The outside world does not see it.

The developer communicates and controls the object via the Public Interface, a set of Public Properties and Methods which the object exposes to the outside. How an object performs its tasks is of no concern to the developer using the object. It is essentially a "Black Box".

Inheritance

In the pure world of OOP, objects can be created from other objects. Any properties and methods from one class are then automatically available as properties and methods of the new class. This is called Inheritance.

Visual Basic provides a modified version of Inheritance commonly termed Containment. A class can still expose its childrens' properties and methods but the developer of the class must explicitly reference the child in any calls made to those properties or methods. A comparison is the best way of explaining this concept.

Figure 3.1a : C++ directly uses Person.Name when Student.Name is called.

Figure 3.1b : VB must explicitly call Person.Name when Student.Name is called.

Let's look at how you would code this :

 'Class : clsStudent
 ' Contains a reference to a Person Object

 'private member variables
 Private oPerson as clsPerson
 Private iGrade as Integer

 Public Sub Create ( )
 'load this object and create the person object

      Set oPerson = New clsPerson

 End Sub

 Public Property Let Name ( sNewName as String)
 'set the person objects Name Property

      oPerson.Name = sNewName

 End Property

 Public Property Get Name ( ) as String
 'retreive the person objects Name Property

      Name = oPerson.Name

 End Property

Polymorphism

Objects can be as complex as you desire to make them. Some objects provide the functionality of several different objects with similar behaviours. This is Polymorphism, the ability to change the behaviour of a class based upon certain criteria.

Lets look at an example :

Two geographic areas have different methods to calculate Sales Tax.

Region A calculates Sales Tax at a flat 5% of the Sales Total.

Region B calculates Sales Tax on a prorated basis - 6% up to $200, 4% for all sales over $200.

We can create an object class which performs the calculation for Region A, and another class for Region B. Then create a class which references both the previous objects. When you wish to calculate taxes just tell your Wrapper Class which region to use and the proper calculations are performed.

Here's the code for this example :


 'Class : clsTaxRegionA

 Const iTaxRate = .05

 Public function CalcTaxes ( curTaxableValue as Currency) as Currency

      CalcTaxes = iTaxRate * curTaxableValue

 End Function
 'Class : clsTaxRegionB

 Const iTaxRate1 = .06
 Const iTaxRate2 = .04

 Public function CalcTaxes ( curTaxableValue as Currency) as Currency

 If curTaxValue < 200 then

      CalcTaxes = iTaxRate1 * curTaxableValue

 else

      CalcTaxes = iTaxRate2 * curTaxableValue

 end if

 End Function
 'Class : clsSalesTax

 'private member variables
 Private sRegion as String
 Private oRegionA as clsTaxRegionA
 Private oRegionB as clsTaxRegionB

 Public Property Let Region ( sRegionFlag as String )

      sRegion = sRegionFlag

 End Property

 Public Sub Create ( )

      Set oRegionA = New clsTaxRegionA
      Set oRegionB = New clsTaxRegionB

 End Sub

 Public function CalcTaxes ( curTaxableValue as Currency) as Currency
 'calculate taxes based on region

 if sRegion = "A" then

      CalcTaxes = oRegionA.CalcTaxes (curTaxableValue)

 else

      CalcTaxes = oRegionB.CalcTaxes (curTaxableValue)

 end if

 End Function

[Lesson 1 , Lesson 2]




Home | About | What's New | Source Code | FAQ | Tips & Tricks | Downloads | ToolBox | Tutorials | Game Programming | VB Award | Search | VB Forums | Feedback | VBNews | Copyright & Disclaimer | Advertise | Privacy Policy |

Quick searches: Site Search | Advanced Site Search 

Copyright 2002 by Exhedra Solutions, Inc.
By using this site you agree to its terms and conditions
VB Explorer and VBExplorer.com are trademarks of Exhedra Solutions, Inc.