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.

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

41<br />

42 ' calculate CCircle diameter<br />

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

44 Return mRadius * 2<br />

45 End Function ' Diameter<br />

46<br />

47 ' calculate CCircle4 circumference<br />

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

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

50 End Function ' Circumference<br />

51<br />

52 ' calculate CCircle4 area<br />

53 Public Overridable Function Area() As Double<br />

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

55 End Function ' Area<br />

56<br />

57 ' return String representation of CCircle4<br />

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

59<br />

60 ' use MyBase reference <strong>to</strong> return CPoint String representation<br />

61 Return "Center= " & MyBase.ToString() & _<br />

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

63 End Function ' ToString<br />

64<br />

65 End Class ' CCircle4<br />

Fig. 9.12 CCircle4 class that inherits from class CPoint, which does not provide<br />

Protected data (part 2 of 2).<br />

For the purpose of this example, <strong>to</strong> demonstrate both explicit and implicit calls <strong>to</strong> baseclass<br />

construc<strong>to</strong>rs, we include a second construc<strong>to</strong>r that calls the base-class construc<strong>to</strong>r<br />

explicitly. Lines 17–23 declare the CCircle4 construc<strong>to</strong>r that invokes the second<br />

CPoint construc<strong>to</strong>r explicitly using the base-class construc<strong>to</strong>r-call syntax (i.e., reference<br />

MyBase followed by a set of parentheses containing the arguments <strong>to</strong> the base-class construc<strong>to</strong>r).<br />

In this case, xValue and yValue are passed <strong>to</strong> initialize the base-class members<br />

mX and mY. The insertion of the MyBase reference followed by the dot opera<strong>to</strong>r<br />

accesses the base-class version of that method—in this construc<strong>to</strong>r, MyBase.New invokes<br />

the CPoint construc<strong>to</strong>r explicitly (line 21). By making this explicit call, we can initialize<br />

mX and mY <strong>to</strong> specific values, rather than <strong>to</strong> 0. When calling the base-class construc<strong>to</strong>r<br />

explicitly, the call <strong>to</strong> the base-class construc<strong>to</strong>r must be the first statement in the derivedclass-construc<strong>to</strong>r<br />

definition.<br />

Common <strong>Program</strong>ming Error 9.2<br />

It is a syntax error if a derived class uses MyBase <strong>to</strong> call its base-class construc<strong>to</strong>r, and the<br />

arguments do not match exactly the parameters specified in one of the base-class construc<strong>to</strong>r<br />

definitions. 9.2<br />

Class CCircle4’s ToString method (line 58–63) overrides class CPoint’s<br />

ToString method (lines 54–56 of Fig. 9.4). As we discussed earlier, overriding this<br />

method is possible, because method ToString of class System.Object (class<br />

CPoint’s base class) is declared Overridable. Method ToString of class<br />

CCircle4 displays the Private instance variables mX and mY of class CPoint by

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

Saved successfully!

Ooh no, something went wrong!