visual basic: an object oriented approach

12
Visual Basic: An Object Oriented Approach 3 – Making Objects Work

Upload: chiquita-lane

Post on 01-Jan-2016

19 views

Category:

Documents


2 download

DESCRIPTION

Visual Basic: An Object Oriented Approach. 3 – Making Objects Work. Object Structure. An Object has an internal structure that defines its state or identity has an interface through which other objects can work with it. Internal Structure and Operations. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Visual Basic: An Object Oriented Approach

Visual Basic: An Object Oriented Approach

3 – Making Objects Work

Page 2: Visual Basic: An Object Oriented Approach

Object Structure An Object

has an internal structure that defines its state or identity

has an interface through which other objects can work with it

Internal structure of anobject

Object Interface

Property

Definitions

Method

Definitions

Object User(a program)

Page 3: Visual Basic: An Object Oriented Approach

Internal Structure and Operations

The state of an object is the set of values of its attributes these are stored in variables, whose value can change over time

they can be altered or manipulated by operations, known as program statements

2000 197525

Age = CurrentYear – YearOfBirth

Page 4: Visual Basic: An Object Oriented Approach

Operators Operators work on values and

variables to perform simple arithmetic (+, -, *, / …) string (text) manipulation date manipulation other forms of calculation assignment (=) comparison (=, >, <, >=, <=, Like)

Values, variables and operators can be put together (using a form of grammar) to form expressions

Page 5: Visual Basic: An Object Oriented Approach

Expressions

An expression is a combination of values,

variables and operators has a value (found by

calculating the expression) has a type of value (e.g.

number, string, date…) can be a direct

replacement for a value of the appropriate type

x+2

“Joe” & “ Bloggs”

Date + 7 (1 week from

today)

z * (x + 2) (=z * 3 if x is 1)

x+2

“Joe” & “ Bloggs”

Date + 7 (1 week from

today)

z * (x + 2) (=z * 3 if x is 1)

Page 6: Visual Basic: An Object Oriented Approach

Conditions Conditions are

expressions that evaluate to True or False

Use conditions to control what happens next in a program select statements to

execute repeat certain statements

until a condition is True

If age >= 18 Thenbuy beer

Elsebuy soft drink

End If

If age >= 18 Thenbuy beer

Elsebuy soft drink

End If

Dosit driving test

Loop Until Result=“Pass”

Dosit driving test

Loop Until Result=“Pass”

Page 7: Visual Basic: An Object Oriented Approach

Abstraction

To create a computer program, we must create an abstraction Remove all unnecessary details Retain all info necessary to the calculations

C ir c u m f e r e n c e ,C = * d i a m e t e r

R o t a t i o n a l v e l o c i t y , i n r e v s p e r m i n u t e

S p e e d = * C = * * d i a m e te r (i n m e t r e s p e r m i n u te ), o r . .

S p e e d = * * d i a m e te r * 6 0 / 1 0 0 0 (i n k m p e r h o u r )

Page 8: Visual Basic: An Object Oriented Approach

Object Orientation In Object-Oriented programs, Objects

are abstractions of real-world things We often refer to this a Modelling

An object is a model of something we represent in a program

The object Interface is how we manipulate it

BankAccount

BalanceDepositWithdraw

Deposit Code

Withdraw Code

Balance Variable

Messages fromObject User

Page 9: Visual Basic: An Object Oriented Approach

Statements A statement is the fundamental

syntactical element of a program smallest piece of code that the language will

accept in its own right A statement can be used to

set aside space for data storage (variables) assign a value to a variable perform a calculation and assign its result to

a variable execute a previously-defined operation control other statements

Page 10: Visual Basic: An Object Oriented Approach

Example statements

Dim x As Integer, y As Single

x = 2

x = x + 4

If x > 3 Then

y = Sqr(x)

Print y

End If

Declares variables

Assigns a value to the variable

Assigns the result of an expression

Controls other statements

Executes a pre-defined operation

Page 11: Visual Basic: An Object Oriented Approach

Statements in Objects Operations in objects are sequences of statements

Can change the state of the object by changing the value of one or more variables

Can retrieve data from the object Could do both

In general A Sub is a sequence of statements that does something to

the state of an object A Function is a sequence of statements that retrieves a

value based on the values of the object’s member variables

A Property is a set of statements that reflect an attribute of the object, allowing it to be read or changed

Sometimes, these distinctions can become blurred

Page 12: Visual Basic: An Object Oriented Approach

Inter-object communication

One object (the Client) makes use of another object (the Server) by sending it a message

The message is a statement within one of the Client’s methods (operations).

Account.Deposit 100.00Account.Deposit 100.00

(Account)(ATM)Deposit

(100.00)

This statement is an operation in the

ATM object