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

Create successful ePaper yourself

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

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

72 ' calculate CCircle diameter<br />

73 Public Function Diameter() As Double<br />

74 Return mRadius * 2<br />

75 End Function ' Diameter<br />

76<br />

77 ' calculate CCircle circumference<br />

78 Public Function Circumference() As Double<br />

79 Return Math.PI * Diameter()<br />

80 End Function ' Circumference<br />

81<br />

82 ' calculate CCircle area<br />

83 Public Function Area() As Double<br />

84 Return Math.PI * mRadius ^ 2<br />

85 End Function ' Area<br />

86<br />

87 ' return String representation of CCircle<br />

88 Public Overrides Function ToString() As String<br />

89 Return "Center = " & "[" & mX & ", " & mY & "]" & _<br />

90 "; Radius = " & mRadius<br />

91 End Function ' ToString<br />

92<br />

93 End Class ' CCircle<br />

Fig. 9.6 CCircle class contains an x-y coordinate and a radius (part 3 of 3).<br />

Module modCircleTest (Fig. 9.7) tests class CCircle. Line 12 instantiates an<br />

object of class CCircle, assigning 37 as the x-coordinate value, 43 as the y-coordinate<br />

value and 2.5 as the radius value. Lines 15–17 use properties X, Y and Radius <strong>to</strong> retrieve<br />

these values, then concatenate the values <strong>to</strong> String output. Lines 20–22 use<br />

CCircle’s X, Y and Radius properties <strong>to</strong> change the x-y coordinate pair value and radius<br />

value, respectively. Property Radius ensures that member variable mRadius cannot be<br />

assigned a negative value. Line 27 calls CCircle’s ToString method <strong>to</strong> obtain the<br />

CCircle’s String representation, and lines 31–38 call CCircle’s Diameter, Circumference<br />

and Area methods.<br />

After writing all the code for class CCircle (Fig. 9.6), note that a major portion of<br />

the code in this class is similar, if not identical, <strong>to</strong> much of the code in class CPoint. For<br />

example, the declaration in CCircle of Private variables mX and mY and properties X<br />

and Y are identical <strong>to</strong> those of class CPoint. In addition, the class CCircle construc<strong>to</strong>rs<br />

and method ToString are almost identical <strong>to</strong> those of class CPoint, except that they<br />

also supply mRadius information. In fact, the only other additions <strong>to</strong> class CCircle are<br />

Private member variable mRadius, property Radius and methods Diameter,<br />

Circumference and Area.<br />

1 ' Fig. 9.7: CircleTest.vb<br />

2 ' Testing class CCircle.<br />

3<br />

4 Imports System.Windows.Forms<br />

5<br />

Fig. 9.7 modCircleTest demonstrates class CCircle functionality<br />

(part 1 of 2).

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

Saved successfully!

Ooh no, something went wrong!