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.

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

33 Set(ByVal radiusValue As Double)<br />

34<br />

35 If radiusValue > 0<br />

36 mRadius = radiusValue<br />

37 End If<br />

38<br />

39 End Set<br />

40<br />

41 End Property ' Radius<br />

42<br />

43 ' calculate CCircle2 diameter<br />

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

45 Return mRadius * 2<br />

46 End Function ' Diameter<br />

47<br />

48 ' calculate CCircle2 circumference<br />

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

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

51 End Function ' Circumference<br />

52<br />

53 ' calculate CCircle2 area<br />

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

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

56 End Function ' Area<br />

57<br />

58 ' return String representation of CCircle2<br />

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

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

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

62 End Function ' ToString<br />

63<br />

64 End Class ' CCircle2<br />

Fig. 9.8 CCircle2 class that inherits from class CPoint (part 2 of 2).<br />

Lines 12 and 20 in the CCircle2 construc<strong>to</strong>rs (lines 10–24) invoke the default<br />

CPoint2 construc<strong>to</strong>r implicitly <strong>to</strong> initialize the base-class portion (variables mX and mY,<br />

inherited from class CPoint) of a CCircle2 object <strong>to</strong> 0. <strong>How</strong>ever, because the parameterized<br />

construc<strong>to</strong>r (lines 17–24) should set the x-y coordinate <strong>to</strong> a specific value, lines 21–<br />

22 attempt <strong>to</strong> assign argument values <strong>to</strong> mX and mY directly. Even though lines 21–22<br />

attempt <strong>to</strong> set mX and mY values explicitly, line 20 first calls the CPoint default construc<strong>to</strong>r<br />

<strong>to</strong> initialize these variables <strong>to</strong> their default values. The compiler generates a syntax<br />

error for lines 21–22 (and line 60, where CCircle2’s method ToString attempts <strong>to</strong> use

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

Saved successfully!

Ooh no, something went wrong!