Is2215 lecture4 student (1)

Preview:

DESCRIPTION

 

Citation preview

SOME MORE EXAMPLESDOING IT IN CODE

ATMEXAMPLE 1

The Problem

You are required to create an ATM application using OOP

Requirements: User can deposit money User can withdraw money User can view balance

Example #1

Creating the Class In Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Declare Methods

Create constructors as needed

Create a destructor, if appropriate

1. Add Class to the Project

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

2. Provide Appropriate Name

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

3. Declare Properties

What does it look like?

What are its attributes?

What characteristics does it have?

3. Declare Properties

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

4. Property Procedures

This property is

ReadOnly

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

3. Create Methods

What does it do?

How does it behave?

What action does it perform?

5. Deposit

5. Withdraw

Creating Classes in Code

Add a class to the project

Provide appropriate name for the class

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

BANK ACCOUNTEXAMPLE 2

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

1.Declare Properties

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

2. _name

Retrieve Value

Store Value

2. _balance

2. _MaximumWithdrawal

Static (Shared) Members Data and behaviours that pertain to

class as a whole Not just an individual instance of a

class You can access static members even

if no class exists

2. Shared _Rate

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

Deposit()

Overloading Methods A method might need different

parameter in different circumstances The solution is to define overloaded

methods Methods have the same name –

common intent Each method can have a unique

signature In previous example method takes

parameter as a decimal number

Deposit ()

Overloading Methods The overloaded method had the

same name The parameter list was different Accepts an amount as a String (text)

Converts it to a decimal

Withdraw ()

CalculateInterest()

IncreaseRate ()

Declare properties

Property Procedures

Create Methods

Add constructors/destructors if needed

4. Overloading Constructors

In Use

Dim account as New _ BankAccount()

Overloading Constructors

In Use

Dim account as New _ BankAccount(“Joe”)

Overloading Constructors

In Use

Dim account as New _ BankAccount(“Joe”, 1000)

4. Overriding Destructor

Instantiating the Object

Using the Class

Create Object

Withdrawing Cash

Method

InputBox

Withdrawing Cash

Method

Depositing Cash

Calculate Interest

Method

Val(txtRate.Text) / 100).ToString( "C"))

Advanced OOP

Extending our Example

Accessibility LevelsAccess Level Visual Basic Access

Modifier

Unrestricted accessibility

Public

Accessible by containing class

Private

Accessible in current assembly

Friend

Accessible by containing class and derived class

Protected

Accessible in current assembly and by derived classes

Protected Friend

Inheritance

Advantages Developer Productivity Real-world modelling Consistency

BankAccount

SavingsAccount

CheckingAccount

Base class

Derived Classes

Inheritance

A base class can define overridable methods and properties Base class provides the default

implementation Derived class provides alternative

implementations A derived class can:

Inherit base class members Override base class members Shadow base class members

Client application

Polymorphism enables an application to use instances of derived classes interchangeablyPolymorphism enables an application to use instances of derived classes interchangeably

Deposit

Deposit

A SavingsAccount

A CheckingAccount

Benefits of polymorphism:Benefits of polymorphism:

Consistency and simplicity

Extensibility and resilience

Consistency and simplicity

Extensibility and resilience

Base Class Properties

Base Class Properties

Base Class Properties

Base Class Methods

Base Class Methods

Derived Class

Derived Class

Derived Class

Shadowing

To indicate a derived class member shadows a base class member Use the Shadows Keyword

ExamplePublic Class BankAccount

Protected ReadOnly Property _

Balance() As Decimal

End Property

Public Class SavingsAccount

Public Shadows Sub Balance( _

ByVal amount As Decimal)

‘Implementation

End Sub

Combining and Overloading

Combining and Overloading

Implement the Class