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.

418 Object-Oriented <strong>Program</strong>ming: Polymorphism Chapter 10<br />

29<br />

30 ' employee reference <strong>to</strong> a CCommissionWorker<br />

31 employee = commissionWorker<br />

32 output &= GetString(employee) & _<br />

33 commissionWorker.ToString() & " earned " & _<br />

34 commissionWorker.Earnings.ToString("C") & vbCrLf & vbCrLf<br />

35<br />

36 ' employee reference <strong>to</strong> a CPieceWorker<br />

37 employee = pieceWorker<br />

38 output &= GetString(employee) & pieceWorker.ToString() & _<br />

39 " earned " & pieceWorker.Earnings.ToString("C") _<br />

40 & vbCrLf & vbCrLf<br />

41<br />

42 ' employee reference <strong>to</strong> a CHourlyWorker<br />

43 employee = hourlyWorker<br />

44 output &= GetString(employee) & _<br />

45 hourlyWorker.ToString() & " earned " & _<br />

46 hourlyWorker.Earnings.ToString("C") & vbCrLf & vbCrLf<br />

47<br />

48 MessageBox.Show(output, "Demonstrating Polymorphism", _<br />

49 MessageBoxBut<strong>to</strong>ns.OK, MessageBoxIcon.Information)<br />

50 End Sub ' Main<br />

51<br />

52 ' return String containing employee information<br />

53 Shared Function GetString(ByVal worker As CEmployee) As String<br />

54 Return worker.ToString() & " earned " & _<br />

55 worker.Earnings.ToString("C") & vbCrLf<br />

56 End Function ' GetString<br />

57<br />

58 End Class ' CTest<br />

Fig. 10.14 CTest class tests the CEmployee class hierarchy (part 2 of 2).<br />

Line 12 assigns <strong>to</strong> CBoss reference boss a CBoss object and passes <strong>to</strong> its construc<strong>to</strong>r<br />

the boss’s first name (“John”), last name (“Smith”) and fixed weekly salary<br />

(800). Line 26 assigns the derived-class reference boss <strong>to</strong> the base-class CEmployee

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

Saved successfully!

Ooh no, something went wrong!