19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

374 <strong>Computer</strong> <strong>Programming</strong> <strong>Concepts</strong> <strong>and</strong> <strong>Visual</strong> <strong>Basic</strong><br />

when a member variable of class A has class B as its type. Inheritance is a process by which<br />

one class (the child class), inherits the properties, methods, <strong>and</strong> events of another class (the<br />

parent class). <strong>Visual</strong> <strong>Basic</strong> does not support the strict academic definition of inheritance. In this<br />

section, we present programs that illustrate “use” (Example 1) <strong>and</strong> “containment” (Example 2).<br />

In this section we will be setting variables to existing objects. In that case, the proper<br />

statement is<br />

Set objVar = existingObject<br />

EXAMPLE 1<br />

Write a program to create <strong>and</strong> control two airplane objects (referred to as a bomber <strong>and</strong> a plane) <strong>and</strong> a<br />

bomb object. The airplane object should keep track of its location (that is, the number of twips from the<br />

left side <strong>and</strong> the top side of the form), be capable of moving in a direction (Right, Up, or Down) specified<br />

by the user from a combo box, <strong>and</strong> should be able to drop a bomb when so comm<strong>and</strong>ed. The last task<br />

will be carried out by the bomb object. In the event that a bomb dropped from the bomber hits the plane,<br />

the plane should disappear. The airplanes <strong>and</strong> the bomb will have physical representations as pictures<br />

inside image controls. By their locations we mean the upperleft corners of their respective image controls.The<br />

picture files AIRPLANE.BMP <strong>and</strong> BOMB.BMP can be found in the Pictures directory of the<br />

CD accompanying this textbook.<br />

SOLUTION:<br />

Object Property Setting<br />

frmPlanes BackColor (white)<br />

BorderStyle 0-None<br />

WindowState 2-Maximized<br />

imgBomberPic Picture AIRPLANE.BMP<br />

Stretch True<br />

imgPlanePic Picture AIRPLANE.BMP<br />

Stretch True<br />

imgBombPic Picture BOMB.BMP<br />

Stretch True<br />

Visible False<br />

cboDirection List Right<br />

Up<br />

Down<br />

cmdMove Caption Move Bomber<br />

cmdDropBomb Caption Drop Bomb<br />

cmdQuit Caption Quit<br />

‘Class module for CPlane<br />

Private m_imgPlane As Image ‘image control associated with plane<br />

Property Let imagePlane(newPlane As Image)<br />

Set m_imgPlane = newPlane<br />

End Property<br />

Public Function Present() As Boolean<br />

‘Determine if the plane is visible<br />

‘Will be needed by the bomb object.<br />

If m_imgPlane.Visible Then<br />

Present = True<br />

Else<br />

Present = False<br />

End If<br />

End Function

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!