VBA mintapélda - Belépés

Komplex számok osztálya

Option Explicit
' objektum tulajdonságok, állapotok
Private real As Double ' valós rész
Private img As Double, y As Double ' képzetes rész

' konstruktor
Private Sub Class_Initialize()
  real = 0: img = 0
End Sub
  
' destruktor
Private Sub Class_Terminate()
End Sub
  
' privat tagváltozók interface metódusai
Public Property Get RealValue() As Double
  RealValue = real
End Property
  
Public Property Let RealValue(ByVal vNewValue As Double)
  real = vNewValue
End Property
  
Public Property Get ImgValue() As Double
  ImgValue = img
End Property
  
Public Property Let ImgValue(ByVal vNewValue As Double)
  img = vNewValue
End Property
  
' abs név foglalt!!!!
Public Function cabs() As Double
  cabs = Sqr(real * real + img * img)
End Function
  
Public Function conjugalt() As Complex
  Dim c As New Complex
  
  c.RealValue = real: c.ImgValue = -img
  Set conjugalt = c
  Set c = Nothing
End Function
  
' kiírás
Public Sub display(ByVal title As String)
  Dim wstr As String
  wstr = Format(real, "0.00") & " " & Format(img, "0.00") & "i"
  MsgBox wstr, vbOKOnly, title
End Sub
  
' növelés egy másik complex-szel
Public Sub Add(c As Complex)
  real = real + c.RealValue
  img = img + c.ImgValue
End Sub

Program az osztály teszteléséhez

Public Sub test()
Dim c1 As New Complex
Dim c2 As New Complex
Dim c3 As New Complex
Dim c4 As New Complex
Dim c5 As New Complex

c1.display "inicializálás után"
c2.RealValue = 1: c2.ImgValue = 0
c2.display "1, 0i"
c3.RealValue = 0.5: c3.ImgValue = 2.4
c3.display "0,5, 2.4i"
Set c4 = c3.conjugalt()
c4.display "2.4, 0.5i"
Set c5 = c2
c5.Add c3
c5.display "1, 0i + 0.5,2.4i"
End Sub
Utolsó módosítás: 2017. március 9., csütörtök, 19:12