Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

Small OOP example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Small OOP example

    By demand of a simple example of using the object oriented features of PowerBasic 9 !! Here it is.

    This example has two classes cShip and cContainer

    It show the use of collections through the AddContainer and GetContainer
    methods.

    Code:
    ' ======================================================================================================
    
    Class cContainer
        instance m_Width    As Single
        Instance m_Length   As Single
        instance m_height   As Single
    
        Interface iContainer
          inherit iDispatch
    
        Property Get Width () As Single
    
            Property = m_Width
    
        End Property
    
        Property Set Width (ByVal Value As Single)
    
            m_Width = Value
    
        End Property
    
        Property Get Length() As Single
    
            Property = m_Length
    
        End Property
    
        Property Set Length(ByVal Value As Single)
    
            m_Length = Value
    
        End Property
    
        Property Get Height() As single
    
            Property = m_Height
    
        End Property
    
        Property Set Height(ByVal Value As Single)
    
            m_Height = Value
    
        End Property
    
        End Interface
    
    End Class
    
    Class cShip
    
        Instance m_ContainerCount   As Word
        instance m_Volume           As Single
        instance m_VolumeUsed       As Single
    
        Instance m_Containers()     As iContainer
    
        class method create()
    
            ReDim m_Containers(0) As Instance iContainer
    
        End method
    
        Interface iShip
          inherit iDispatch
    
        method SetMaxSize(ByVal nLength As Single, ByVal nWidth As Single, ByVal nHeight As Single)
    
          m_Volume = nLength * nWidth * nHeight
    
        End method
    
        method AddContainer(ByVal iC As iContainer) As Long
    
          Local ContainerVolume As Single
    
          If IsObject(iC) Then
    
             If IC.Width And iC.Height And iC.Length Then
    
                ContainerVolume = iC.Width * iC.Height * iC.Length
    
                If ContainerVolume + m_VolumeUsed <= m_Volume Then
    
                   Incr m_ContainerCount
    
                   ReDim Preserve m_Containers(m_ContainerCount - 1) As Instance iContainer
    
                   m_Containers(m_ContainerCount - 1) = iC
    
                   m_VolumeUsed = m_VolumeUsed + ContainerVolume
    
                   Method = 1
    
                Else
    
                   Method = 0
    
                End If
    
             End If
    
          End If
    
        End Method
    
        method GetVolumeUsed() As Single
    
            Method = m_VolumeUsed
    
        End method
    
        Method GetCapacityLeft() As Single
    
            method = m_Volume - m_VolumeUsed
    
        End Method
    
        method GetContainerCount() As Word
    
          method = m_ContainerCount
    
        End method
    
        method GetContainer(ByVal nItem As Word) As iContainer
    
          If nItem > 0 And nItem <= m_ContainerCount Then
    
             method = m_Containers(nItem - 1)
    
          Else
    
             method = Nothing
    
          End If
    
        End method
    
        End Interface
    End class
    
    
    Function pbmain() As Long
    
     Local cCont  As iContainer
     Local cBoat  As iShip
    
     cBoat = Class "cShip"
     cCont = Class "cContainer"
    
     cBoat.SetMaxSize(20, 20, 40)
    
     cCont.Length = 20
     cCont.Width  = 20
     cCont.Height = 20
    
     cBoat.AddContainer(cCont)
    
     Print cBoat.GetContainerCount()
    
     Local cOneContainer As iContainer
    
     cOneContainer = Class "cContainer"
    
     cOneContainer = cBoat.GetContainer(1)
    
     Print "Width of first container = " & Format$(cOneContainer.Width)
    
     Print "Volume used  : " & Format$(cBoat.GetVolumeUsed) & " square feet"
     Print "Capacity left: " & Format$(cBoat.GetCapacityLeft) & " square feet"
    End Function
    So here we are, this is the end.
    But all that dies, is born again.
    - From The Ashes (In This Moment)

  • #2
    Thanks for the example, Steven. Like many here, I'm sure, am very curious about OOP. I've converted it to PBWin (I don't have CC):

    Code:
    'PBWIN 9.00 - WinApi 05/2008 - XP Pro SP3
    'OOP example by Steve Pringles
    ' ======================================================================================================
    Class cContainer
        Instance m_Width    As Single
        Instance m_Length   As Single
        Instance m_height   As Single
        Interface iContainer
          Inherit IDispatch
        Property Get Width () As Single
            Property = m_Width
        End Property
        Property Set Width (ByVal Value As Single)
            m_Width = Value
        End Property
        Property Get Length() As Single
            Property = m_Length
        End Property
        Property Set Length(ByVal Value As Single)
            m_Length = Value
        End Property
        Property Get Height() As Single
            Property = m_Height
        End Property
        Property Set Height(ByVal Value As Single)
            m_Height = Value
        End Property
        End Interface
    End Class
    Class cShip
        Instance m_ContainerCount   As Word
        Instance m_Volume           As Single
        Instance m_VolumeUsed       As Single
        Instance m_Containers()     As iContainer
        Class Method Create()
            ReDim m_Containers(0) As Instance iContainer
        End Method
        Interface iShip
          Inherit IDispatch
        Method SetMaxSize(ByVal nLength As Single, ByVal nWidth As Single, ByVal nHeight As Single)
          m_Volume = nLength * nWidth * nHeight
        End Method
        Method AddContainer(ByVal iC As iContainer) As Long
          Local ContainerVolume As Single
          If IsObject(iC) Then
             If IC.Width And iC.Height And iC.Length Then
                ContainerVolume = iC.Width * iC.Height * iC.Length
                If ContainerVolume + m_VolumeUsed <= m_Volume Then
                   Incr m_ContainerCount
                   ReDim Preserve m_Containers(m_ContainerCount - 1) As Instance iContainer
                   m_Containers(m_ContainerCount - 1) = iC
                   m_VolumeUsed = m_VolumeUsed + ContainerVolume
                   Method = 1
                Else
                   Method = 0
                End If
             End If
          End If
        End Method
        Method GetVolumeUsed() As Single
            Method = m_VolumeUsed
        End Method
        Method GetCapacityLeft() As Single
            Method = m_Volume - m_VolumeUsed
        End Method
        Method GetContainerCount() As Word
          Method = m_ContainerCount
        End Method
        Method GetContainer(ByVal nItem As Word) As iContainer
          If nItem > 0 And nItem <= m_ContainerCount Then
             Method = m_Containers(nItem - 1)
          Else
             Method = Nothing
          End If
        End Method
        End Interface
    End Class
    
    Function PBMain() As Long
     Local cCont  As iContainer
     Local cBoat  As iShip
     Local s$
     cBoat = Class "cShip"
     cCont = Class "cContainer"
     cBoat.SetMaxSize(20, 20, 40)
     cCont.Length = 20
     cCont.Width  = 30
     cCont.Height = 20
     cBoat.AddContainer(cCont)
     s$ =  "Str$(cBoat.GetContainerCount()) = " & Str$(cBoat.GetContainerCount()) & $CrLf 
     Local cOneContainer As iContainer
     cOneContainer = Class "cContainer"
     cOneContainer = cBoat.GetContainer(1)
     s$ = s$ & "Width of first container = " & Format$(cOneContainer.Width) & $CrLf 
     s$ = s$ &  "Volume used  : " & Format$(cBoat.GetVolumeUsed) & " square feet" & $CrLf 
     s$ = s$ &  "Capacity left: " & Format$(cBoat.GetCapacityLeft) & " square feet"   & $CrLf 
     ?s$
    End Function
    I'm sorry but I just don't see any inherent advantage using OOP, at least with this example. It appears to me the same job could be done in PB Win with a lot fewer lines (and be more readable). Not trying to argue, just trying to understand.
    It's a pretty day. I hope you enjoy it.

    Gösta

    JWAM: (Quit Smoking): http://www.SwedesDock.com/smoking
    LDN - A Miracle Drug: http://www.SwedesDock.com/LDN/

    Comment


    • #3
      Gosta,

      Yes, this example I gave doesn't present the actual benefits over procedural
      programming. However, consider software where working in a team is demanded. With classes, it's way easier to divide the tasks at hand than with procedural programming paradigms.

      Also, classes allow you to extend the functionallity of a program seemlessly. Meaning that one can add new methods when there is a need for it. Try to
      do that with procedural programming. like adding a new parameter to a function. The program is broken, you need to change the calls to the function.

      In essence, object oriented programming won't allow you to do things that can't be done with procedural programming but when a program grows, it may just become a bit easier to debug. Especially when you have a design using UML.

      Cheers

      Steven
      So here we are, this is the end.
      But all that dies, is born again.
      - From The Ashes (In This Moment)

      Comment

      Working...
      X