Announcement

Collapse
No announcement yet.

Objectification and Orientedness

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

  • Objectification and Orientedness



    Hi. I am toying with the implementation of OOPS in PowerBASIC.
    I submit the following code for your suggestions and criticisms:

    They are patterned after examples in the book "Teach yourself C++"
    by Al Stevens.

    Code:
    Rem Ex-07-01.bas
    Rem PBCC  4.04.0042
    
    #Compile Exe
    #Dim All
    
    #Include "Ex-07-01.inc"
    
    Function PBMain () As Long
    
    	Local lresult As Long
    	Local thiscube As Cube
    
    	Constructor(thiscube,7,8,9)
    	Volume(thiscube)
    	Print "volume = " + Str$(lresult)
    	Destructor(thiscube)
    	WaitKey$
    	
    End Function
    Code:
    Rem ---------- a Cube class
    Rem Ex-07-01.inc
    Rem PBCC  4.04.0042
    
    Macro CLASS=Type
    
    Macro Constructor(mycube, lht,lwd,ldp)
    	mycube.m__Cube = CodePtr(Cube__Cube)		' Constructor.
    	mycube.m___Cube = CodePtr(Cube___Cube)		' Destructor.
    	mycube.m__volume = CodePtr(Cube__volume)	' Volume.
    	Call Dword mycube.m__Cube Using Cube__Cube(mycube, lht,lwd,ldp)
    End Macro
    
    Macro Destructor(mycube)
    	Call Dword mycube.m___Cube Using Cube___Cube(mycube)
    End Macro
    
    Macro volume(mycube)
    '  Corrected 07-22-08
    '   Call Dword thiscube.m__volume Using Cube__volume(thiscube) To lresult
        Call Dword mycube.m__volume Using Cube__volume(mycube) To lresult
    End Macro
    
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    CLASS Cube
       m_lheight As Long
       m_lwidth  As Long
       m_ldepth As Long
       m__cube As Dword		' Constructor.
       m___cube As Dword		' Destructor.
       m__volume As Dword	' Member function (compute volume).
    End CLASS
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' Constructor.
    Sub Cube__Cube(mycube As Cube, lheight As Long, lwidth As Long, ldepth As Long)
    
    	Print "Constructor"
    	mycube.m_lheight = lheight
    	mycube.m_lwidth  = lwidth
    	mycube.m_ldepth  = ldepth
    
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' Destructor.
    Sub Cube___Cube(mycube As Cube)
    	Print "Destructor"
    End Sub
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' Member function (compute volume).
    Function Cube__volume(mycube As Cube) As Long
    
    	Print "Cube__volume"
    	Function = mycube.m_lheight * mycube.m_lwidth * mycube.m_ldepth
    
    End Function
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Edit: 7-22-08 - Changed "thiscube" to "mycube" in the macro "volume(mycube)".
    Last edited by Robert DeBolt; 22 Jul 2008, 06:24 PM. Reason: 7-22-08 - Changed "thiscube" to "mycube" in the macro "volume(mycube)".
    Regards,
    Bob

  • #2
    Your example workedOK, however, I had hoped for two different results when I tried two cubes?
    Code:
    FUNCTION PBMAIN () AS LONG
    
        LOCAL lresult AS LONG
        LOCAL thiscube AS Cube
        LOCAL thiscube2 AS Cube
    
        Constructor(thiscube,7,8,9)
        Constructor(thiscube2,27,28,29)
        Volume(thiscube)
        PRINT "volume = " + STR$(lresult)
        Destructor(thiscube)
        Volume(thiscube2)
        PRINT "volume = " + STR$(lresult)
        Destructor(thiscube2)
        WAITKEY$
    
    END FUNCTION

    Comment


    • #3
      Hi,

      thanks for sharing this interesting experiment.

      I think it is pretty nice, on other side I am not sure if constructor/destructor can remain with the same simple name in case of multiple classes involved.

      So maybe it should be Cube__Constructor / Cube__Destructor, or similar according to class it belongs to.


      Petr

      P.S. Very interesting is that PB editor highlights the CLASS keyword, hmm
      [email protected]

      Comment


      • #4
        Thanks, David, for your comment.
        I had inadvertantly typed "thiscube" instead of "mycube" in the "volume" macro. The source above has been corrected. Please try again.

        Originally posted by Petr Schreiber jr View Post
        I am not sure if constructor/destructor can remain with the same simple name in case of multiple classes involved.

        So maybe it should be Cube__Constructor / Cube__Destructor, or similar according to class it belongs to.

        P.S. Very interesting is that PB editor highlights the CLASS keyword, hmm
        Yes, I agree with you ,Petr. We should have some means to differentiate a cube's volume versus a sphere's volume. However, "Cube__Constructor / Cube__Destructor / Cube_volume" feels to be too much a function call rather than a message. Besides, Cube_volume returns a mysterious value which is hidden from the programmer's view.

        I prefer a syntax more like the following:

        Code:
        	Xt.MATTRN(X)		' X'
        	XtX.MATMULT(Xt,X)	' X'X
        	XtXinv.MATINV(XtX)      ' (X'X)^-1
        which may be viewed as "send the MATTRN message to Xt using X."
        Or "send the MATMULT message to XtX using Xt and X."

        P.S. The Help screen shows that "CLASS" refers to "CLSID$ function" but "CLASS" is not itself a reserved word.
        Regards,
        Bob

        Comment

        Working...
        X