30.07.2013 Views

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

Visual Basic.NET How to Program (PDF)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

358 Object-Oriented <strong>Program</strong>ming: Inheritance Chapter 9<br />

21 ' display new point value<br />

22 output &= vbCrLf & vbCrLf & _<br />

23 "The new location of point is " & point.ToString()<br />

24<br />

25 MessageBox.Show(output, "Demonstrating Class Point")<br />

26 End Sub ' Main<br />

27<br />

28 End Module ' modPointTest<br />

Fig. 9.5 modPointTest demonstrates class CPoint functionality (part 2 of 2).<br />

We now discuss the second part of our introduction <strong>to</strong> inheritance by creating and<br />

testing class CCircle (Fig. 9.6), which directly inherits from class System.Object<br />

and represents an x-y coordinate pair (representing the center of the circle) and a radius.<br />

Lines 7–8 declare the instance variables mX, mY and mRadius as Private data. The<br />

Public services of class CCircle include two CCircle construc<strong>to</strong>rs (lines 11–27),<br />

properties X, Y and Radius (lines 30–70), methods Diameter (lines 73–75), Circumference<br />

(lines 78–80), Area (lines 83–85) and ToString (lines 88–91). These properties<br />

and methods encapsulate all necessary features (i.e., the “analytic geometry”) of a<br />

circle; in the next section, we show how this encapsulation enables us <strong>to</strong> reuse and extend<br />

this class.<br />

1 ' Fig. 9.6: Circle.vb<br />

2 ' CCircle class contains x-y coordinate pair and radius.<br />

3<br />

4 Public Class CCircle<br />

5<br />

6 ' coordinate of center of CCircle<br />

7 Private mX, mY As Integer<br />

8 Private mRadius As Double ' CCircle's radius<br />

9<br />

10 ' default construc<strong>to</strong>r<br />

11 Public Sub New()<br />

12<br />

13 ' implicit call <strong>to</strong> Object construc<strong>to</strong>r occurs here<br />

14 X = 0<br />

15 Y = 0<br />

16 Radius = 0<br />

17 End Sub ' New<br />

18<br />

Fig. 9.6 CCircle class contains an x-y coordinate and a radius (part 1 of 3).

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

Saved successfully!

Ooh no, something went wrong!