Announcement

Collapse
No announcement yet.

Objectification and Orientedness

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

  • Objectification and Orientedness



    Here is my attempt to experiment with OOPS with help from David L Morris
    and Petr Schreiber jr.

    These examples come from Teach Yourself C++ by Al Stevens.

    Code:
    REM Ex-07-01-DavidLMorris.bas
    
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "Ex-07-01.inc"
    
    FUNCTION PBMAIN () AS LONG
    
       LOCAL thiscube AS Cube
       LOCAL thiscube2 AS Cube
    
       Cube__Cube(thiscube,7,8,9)
       PRINT "volume = " + STR$(Cube__volume(thiscube))
       Cube___Cube(thiscube)
    
    	PRINT
       Cube__Cube(thiscube2,27,28,29)
       PRINT "volume = " + STR$(Cube__volume(thiscube2))
       Cube___Cube(thiscube2)
       WAITKEY$
    
    END FUNCTION
    Code:
    ' ---------- a Cube class
    ' Ex-07-01.inc
    
    MACRO CLASS=TYPE
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    CLASS Cube
       m_lheight AS LONG
       m_lwidth  AS LONG
       m_ldepth AS LONG
    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
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The extra print statements are simply to show the path through the class methods.

    Enjoy!


    Regards,
    Bob

  • #2
    The Cube class with inline functions.

    Code:
    ' Ex-07-02A.bas
    ' The Cube class with inline functions.
    
    #COMPILE EXE
    #DIM ALL
    
    #INCLUDE "Ex-07-02A.inc"
    
    FUNCTION PBMAIN () AS LONG
    
    	COLOR 10,0
       LOCAL thiscube AS Cube
    
       Cube__Cube(thiscube,7,8,9)   ' declare a Cube.
       PRINT "thiscube volume = " + STR$(Cube__volume(thiscube))
       Cube___Cube(thiscube)      ' destructors.
       WAITKEY$
    
    END FUNCTION
    Code:
    ' Ex-07-02A.inc
    ' The Cube class with inline functions.
    '
    ' ---------- a Cube class
    
    MACRO CLASS=TYPE
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    CLASS Cube
       m_lheight AS LONG
       m_lwidth  AS LONG
       m_ldepth AS LONG
    END CLASS
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' ----- Constructor function.
    MACRO Cube__Cube(mycube, lheight, lwidth, ldepth)
          PRINT "Constructor"
          mycube.m_lheight = lheight
          mycube.m_lwidth  = lwidth
          mycube.m_ldepth  = ldepth
    END MACRO
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' Destructor.
    MACRO Cube___Cube(mycube)
        PRINT "Destructor"
    END MACRO
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    ' Member function (compute volume).
    MACRO FUNCTION Cube__volume(mycube)
        PRINT "Cube__volume"
    END MACRO = mycube.m_lheight * mycube.m_lwidth * mycube.m_ldepth
    
    '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Regards,
    Bob

    Comment


    • #3
      Constructor with default parameters.

      Code:
      ' Ex-07-03A.bas
      ' Constructor with default parameters.
      
      #COMPILE EXE
      #DIM ALL
      #DEBUG ERROR ON
      'NOTE: #DEBUG ERROR ON must be enabled
      '      in order for default parameters to work
      
      #INCLUDE "Ex-07-03A.inc"
      
      FUNCTION PBMAIN () AS LONG
      
         COLOR 10,0
         LOCAL thiscube AS Cube
         LOCAL defaultcube AS Cube
      
         Cube__Cube(thiscube,7,8,9) ' declare a Cube.
         Cube__Cube(defaultcube)    ' no initializers.
         PRINT "thiscube volume = " + STR$(Cube__volume(thiscube))
         PRINT "defaultcube volume = " + STR$(Cube__volume(defaultcube))
         Cube___Cube(thiscube)      ' destructors.
         Cube___Cube(defaultcube)
         
         WAITKEY$
         
      END FUNCTION
      Code:
      ' Ex-07-03A.inc
      ' Constructor with default parameters.
      ' ---------- a Cube class
      
      MACRO CLASS=TYPE
      
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      CLASS Cube
         m_lheight AS LONG
         m_lwidth  AS LONG
         m_ldepth  AS LONG
      END CLASS
      
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      
      ' Constructor.
      SUB Cube__Cube (mycube AS Cube, _
                     OPTIONAL lheight AS LONG, _
                     OPTIONAL lwidth AS LONG, _
                     OPTIONAL ldepth AS LONG)
         PRINT "Constructor"
         TRY
            mycube.m_lheight = lheight
            mycube.m_lwidth  = lwidth
            mycube.m_ldepth  = ldepth
         CATCH
            mycube.m_lheight = 1    ' Constructor with default parameters.
            mycube.m_lwidth  = 2
            mycube.m_ldepth  = 3
         END TRY
      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
      
      '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      Regards,
      Bob

      Comment


      • #4
        Hi Robert,

        thanks for the examples
        Maybe destructor could contain RESET?

        I originally wanted to suggest ERASE, but it seems that this keyword ( according to documentation ) works on arrays only.


        Petr
        [email protected]

        Comment


        • #5
          Here comes one more experiment,

          Replicant ( blade runnerish ) object.
          The interesting part of this code is probably only the constructor - thanks to MACROs we do not have to use DIM in PBMAIN and we can directly ... construct.


          Bye,
          Petr

          Code:
          #COMPILE EXE
          #DIM ALL
          
          ' Class definition -----------------------------------------------
          MACRO CLASS = TYPE
          
          CLASS TReplicant
            p_Age     AS LONG  ' -- Property age
          END CLASS
          
          MACRO TReplicant_Create(R, age)
            DIM R AS TReplicant
            R.p_Age     = age
          END MACRO
          
          SUB TReplicant_Destroy(R AS TReplicant)
            RESET R
          END SUB
          
          FUNCTION TReplicant_IsAlive(R AS TReplicant) AS LONG
            FUNCTION = IIF( R.p_Age > 4, 0, 1)
          END FUNCTION
          
          SUB TReplicant_CelebrateBirthday(R AS TReplicant)
            INCR R.p_Age
          END SUB
          
          FUNCTION TReplicant_GetAge(R AS TReplicant) AS LONG
            FUNCTION = R.p_Age
          END FUNCTION
          
          ' Main program ---------------------------------------------------
          FUNCTION PBMAIN () AS LONG
          
            ' -- Create my own Replicant object, with initial age 0
            TReplicant_Create(MyOwnNexus6, 0)
          
            ' -- As long as replicant is alive...
            WHILE TReplicant_IsAlive(MyOwnNexus6)
              MSGBOX "Replicant is"+STR$(TReplicant_GetAge(MyOwnNexus6))+" years old now"
              ' -- Lets celebrate birthday ( extra wild celebration including drinking oil )
              TReplicant_CelebrateBirthday(MyOwnNexus6)
            WEND
          
            MSGBOX "Yup, no more birthday, replicant does have short life span :'("
            TReplicant_Destroy(MyOwnNexus6)
          
            ' -- Here, in true OOP, the MyOwnNexus6 would be removed from memory completely
          
          END FUNCTION
          Last edited by Petr Schreiber jr; 6 Aug 2008, 06:00 AM.
          [email protected]

          Comment


          • #6
            There is an error:
            FUNCTION = IIF( R.p_Age >= 4, 0, 1)

            Should be:
            FUNCTION = IIF( R.p_Age > 4, 0, 1)

            Since they have a life-span of 4 years.
            hellobasic

            Comment


            • #7
              Thanks, Petr, for the example.

              Unfortunately, I don't know how to convert the html code to plain text.
              I look forward to looking at it in more detail.

              Regards,
              Bob

              Comment


              • #8
                You are right,
                I corrected it in code above


                Thanks,
                Petr
                [email protected]

                Comment


                • #9
                  Hi Robert,

                  I use the sequence:
                  - mark with mouse
                  - CTRL-C
                  - CTRL-V to PB editor / notepad ...

                  But in case your browser makes this impossible, I put it here in raw form:
                  Code:
                  #COMPILE EXE
                  #DIM ALL
                  
                  ' Class definition -----------------------------------------------
                  MACRO CLASS = TYPE
                  
                  CLASS TReplicant
                    p_Age     AS LONG  ' -- Property age
                  END CLASS
                  
                  MACRO TReplicant_Create(R, age)
                    DIM R AS TReplicant
                    R.p_Age     = age
                  END MACRO
                  
                  SUB TReplicant_Destroy(R AS TReplicant)
                    RESET R
                  END SUB
                  
                  FUNCTION TReplicant_IsAlive(R AS TReplicant) AS LONG
                    FUNCTION = IIF( R.p_Age > 4, 0, 1)
                  END FUNCTION
                  
                  SUB TReplicant_CelebrateBirthday(R AS TReplicant)
                    INCR R.p_Age
                  END SUB
                  
                  FUNCTION TReplicant_GetAge(R AS TReplicant) AS LONG
                    FUNCTION = R.p_Age
                  END FUNCTION
                  
                  ' Main program ---------------------------------------------------
                  FUNCTION PBMAIN () AS LONG
                  
                    ' -- Create my own Replicant object, with initial age 0
                    TReplicant_Create(MyOwnNexus6, 0)
                  
                    ' -- As long as replicant is alive...
                    WHILE TReplicant_IsAlive(MyOwnNexus6)
                      MSGBOX "Replicant is"+STR$(TReplicant_GetAge(MyOwnNexus6))+" years old now"
                      ' -- Lets celebrate birthday ( extra wild celebration including drinking oil )
                      TReplicant_CelebrateBirthday(MyOwnNexus6)
                    WEND
                  
                    MSGBOX "Yup, no more birthday, replicant does have short life span :'("
                    TReplicant_Destroy(MyOwnNexus6)
                  
                    ' -- Here, in true OOP, the MyOwnNexus6 would be removed from memory completely
                  
                  END FUNCTION

                  Petr
                  [email protected]

                  Comment

                  Working...
                  X