Author Topic: Classes, Unions and Arrays Help Please  (Read 20506 times)

0 Members and 3 Guests are viewing this topic.

kryton9

  • Guest
Classes, Unions and Arrays Help Please
« on: June 09, 2012, 11:11:06 PM »
I am trying to figure out what I can and can't do with my classes. I can't get this to compile, tried different things. Is it possible?
Code: OxygenBasic
  1. class cVec2
  2.     public
  3.         'public member vars
  4.        union
  5.         {
  6.             {
  7.                 double x
  8.                 double y
  9.             }
  10.             {
  11.                 double v[2]
  12.             }
  13.         }
  14.     public
  15.         'public methods
  16.        method constructor()
  17.             x = 0
  18.             y = 0
  19.         end method
  20.        
  21.         method constructor(double aX = 0, double aY = 0)
  22.             x = aX
  23.             y = aY
  24.         end method
  25.        
  26.         method constructor(double aV[2] = {0,0})
  27.             v = aV
  28.         end method
  29.        
  30.         method add(double aX = 0, double aY = 0) as cVec2
  31.             cVec2 temp
  32.             temp.x = x + aX
  33.             temp.y = y + aY
  34.             return temp
  35.         end method
  36.        
  37.         'method add(double aV[2] => (0,0)) as cVec2
  38.        method add(double aV[2] = {0,0}) as cVec2
  39.             cVec2 temp
  40.             temp.v[1] = v[1] + aV[1]
  41.             temp.v[2] = v[2] + aV[2]
  42.             return temp
  43.         end method
  44. end class
  45.  
  46. new cVec2 v1
  47. new cVec2 v2 5,10
  48. new cVec2 v3 => (12,24)
  49.  
  50.     print "v1 should be   0,0 :" v1.x ", " v1.y
  51.     print "v2 should be  5,10 :" v2.x ", " v2.y
  52.     print "v3 should be as x,y 12,24 :" v3.x ", " v3.y
  53.     print "v3 should be        12,24 :" v3.v[1] ", " v3.v[2]
  54.  
  55. del v1
  56. del v2
  57. del v3

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #1 on: June 09, 2012, 11:38:50 PM »
Heading off to sleep. Will be dreaming about what we can do in oxygen!!!

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #2 on: June 10, 2012, 12:41:08 AM »
For overloading, something like this: (unions=confusions :))

Code: OxygenBasic
  1.  
  2. class cVec2  
  3.     public  
  4.         'public member vars  
  5.        double x,y  
  6.    public  
  7.         'public methods  
  8.          
  9.         method destructor()  
  10.         end method  
  11.          
  12.         method constructor()  
  13.         end method  
  14.          
  15.         method constructor(double*ax,*ay)  
  16.             x=ax  
  17.             y=ay
  18.         end method  
  19.  
  20.         method constructor(cVec2*v)  
  21.             x=v.x  
  22.             y=v.y
  23.         end method        
  24.          
  25.         method load(double*u,*v)  
  26.             x=u
  27.             y=v
  28.         end method  
  29.          
  30.         method load(cVec2*v)  
  31.             x=v.x
  32.             y=v.y
  33.         end method  
  34.          
  35.         method add(double*u,*v)  
  36.             x+=u
  37.             y+=v
  38.         end method  
  39.          
  40.         method add(cVec2*v)  
  41.             x+=v.x
  42.             y+=v.y
  43.         end method  
  44.          
  45. end class  
  46.  
  47. test:
  48. '====
  49.  
  50. new cVec2 v1  
  51. new cVec2 v2(5,6)
  52. new cVec2 v3(10,20)
  53. new cVec2 v4(v3)
  54.  
  55. print v4.x "," v4.y
  56. v4.add v2
  57. print v4.x "," v4.y
  58.  
  59. del v1  
  60. del v2  
  61. del v3
  62. del v4  
  63.  

Charles

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #3 on: June 10, 2012, 12:32:17 PM »
Handling dynamic vector arrays. This is somewhat excessive for such a simple data structure but it can be used as a model for more complex things like particles.

All members are contained with a single object. Using one buffer, it is a bit like 'TextArray' (many lines, one text)

Code: OxygenBasic
  1.  
  2. type tVec2
  3.     double x,y
  4. end type
  5.  
  6. class cVecArray2  
  7.     private
  8.         string buf
  9.         sys    pb,mx  
  10.    public  
  11.         'public methods  
  12.          
  13.         method destructor()
  14.         buf=""
  15.         pb=0
  16.         end method              
  17.          
  18.         method constructor(sys n)
  19.           this.dim n
  20.         end method  
  21.                
  22.          
  23.         method redim(sys n)
  24.             sys le=len buf
  25.             n*=sizeof tVec2
  26.             if n<le
  27.               buf=left buf,n
  28.             elseif n>le
  29.               buf+=nuls n-le
  30.             end if
  31.             pb=strptr buf
  32.             mx=n
  33.         end method
  34.  
  35.  
  36.         method dim(sys n)
  37.             buf=""
  38.             redim n
  39.         end method
  40.  
  41.  
  42.         method lboud() as sys
  43.           return 0
  44.         end method
  45.  
  46.      
  47.         method ubound() as sys
  48.           return mx-1
  49.         end method
  50.                
  51.          
  52.         method load(sys n,= double x, y)
  53.             sys pn=pb+n*sizeof tVec2
  54.             tVec2 v at pn
  55.             v.x=x
  56.             v.y=y
  57.         end method  
  58.          
  59.         method load(sys m,n)
  60.             sys pm,pn
  61.             pm=pb+m*sizeof tVec2  
  62.             pn=pb+n*sizeof tVec2
  63.             tVec2 vm at pm,vn at pn  
  64.             vm.x=vn.x
  65.             vm.y=vn.y
  66.         end method  
  67.          
  68.         method add(sys n,= double x, y)  
  69.             sys pn=pb+n*sizeof tVec2
  70.             tVec2 v at pn
  71.             v.x+=x
  72.             v.y+=y
  73.         end method  
  74.          
  75.         method add(sys m,n)  
  76.             sys pm,pn
  77.             pm=pb+m*sizeof tVec2  
  78.             pn=pb+n*sizeof tVec2
  79.             tVec2 vm at pm,vn at pn  
  80.             vm.x+=vn.x
  81.             vm.y+=vn.y
  82.         end method
  83.  
  84.         method info(sys n) as string
  85.             sys pn=pb+n*sizeof tVec2
  86.             tVec2 v at pn
  87.             return " " v.x "," v.y
  88.         end method
  89.        
  90.          
  91.         method vectors(sys n) as tVec2 ptr
  92.           return pb+n*sizeof tVec2
  93.         end method
  94.        
  95.          
  96. end class
  97.  
  98. #recordof cVecArray2  
  99.  
  100.  
  101. 'INDEX IS BASE ZERO
  102. '==================
  103.  
  104. new cVecArray2 vv(10) 'PSEUDO-ARRAY
  105.  
  106. 'LOAD DOUBLES
  107. '
  108. vv.load(1)=5.0,6.0
  109. vv.load(2)=10.0,20.0
  110.  
  111. 'TEST REDIM
  112. '
  113. vv.redim(20)
  114.  
  115. 'ADD DOUBLES
  116. '
  117. vv.add(2)=1.0,2.0
  118. print vv.info(2)
  119.  
  120. 'ADD FROM ANOTHER VECTOR MEMBER
  121. '
  122. vv.add(2,1)
  123. print vv.info(2)
  124.  
  125.  
  126. 'ADD SERIES OF DATA
  127. '
  128. let v=vv.vectors(4)        'create vector v pointing to member 4
  129. v<=11,22,33,44,55,66,77,88 'v was automatically created as a tVec2 pointer
  130. print vv.info 5
  131.  
  132. del vv
  133.  

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #4 on: June 10, 2012, 12:52:27 PM »
Thanks Charles for both samples. Will study them as I plan out my project in more detail.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #5 on: June 10, 2012, 02:11:23 PM »
I will file these pieces in the examples\OOP folder. There are a few more tweaks to Oxygen going in, so these examples will have one or two additional features.

Charles

kryton9

  • Guest
Don't know why this isn't working
« Reply #6 on: June 10, 2012, 09:42:52 PM »
Here is my stumper for tonight. I can't see why it is not working. The listing is showing the code in line 19 wrong so I attached the script.
Code: OxygenBasic
  1. 'test cArray.o2bas
  2. 'by Kent Sarikaya 2012
  3.  
  4. ' $ filename "./../_bin/test cArray.exe"
  5. ' #include "../../../inc/RTL32.inc"
  6.  
  7. ' includepath + "./../_inc/"
  8. ' include once "cConsole.inc"
  9.  
  10. ' % cr c.print crlf
  11.  
  12. class cArray
  13.     private
  14.         string array mA[10]
  15.         sys mLength = 10
  16.     public
  17.         method Constructor()
  18.             for i = 1 to mLength
  19.                 mA[i] = ""
  20.             next
  21.         end method
  22.        
  23.         method Destructor()
  24.             freememory mA
  25.         end method
  26.        
  27.         method GetLength() as sys
  28.             return mLength
  29.         end method
  30.        
  31.         method Set(sys aIndex, sys aValue)
  32.             mA[aIndex] = str aValue
  33.         end method
  34.        
  35.         method Set(sys aIndex, string aString)
  36.             mA[aIndex] = aString
  37.         end method
  38.        
  39.         method Get(sys aIndex) as string
  40.             return mA[aIndex]
  41.         end method
  42.     protected
  43.     private
  44. end class
  45.  
  46. 'new cConsole c "test cArray.o2bas"
  47.    new cArray a
  48.    
  49.         string s = "I see aliens."
  50.         a.Set 3, s
  51.         'c.Print a.Get 3
  52.        Print a.Get 3
  53.         'cr
  54.        
  55.         a.Set 4, "I see ufos."
  56.         'c.Print a.Get 4
  57.        Print a.Get 4
  58.         'cr
  59.        a.Set 5, 42
  60.         'c.Print a.Get 5
  61.        Print a.Get 5
  62.         'cr
  63.        'c.WaitKey
  64.    del a
  65. 'del c

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #7 on: June 11, 2012, 12:50:58 AM »
Almost there!

line 14:

string ma[10] ' delete 'array'

line 24:

freememory ma
delete this line. strings and wstrings are memory-managed automatically

You can inspect the record of a class at compile-time, to see if your source code has been understood:

#recordof cArray

Charles

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #8 on: June 11, 2012, 10:51:05 AM »
Thanks Charles!

Quote
freememory ma
delete this line. strings and wstrings are memory-managed automatically
But I should use it for any other type of arrays in a class?


kryton9

  • Guest
Operator Overloading Test
« Reply #9 on: June 11, 2012, 11:42:27 AM »
I was testing operator overloading Charles. Can't get it to work. I see that you have it in some of your samples.

Code: OxygenBasic
  1. string crlf = chr( 13 ) + chr( 10 )
  2.  
  3. class tiny
  4.     'member variables
  5.    private
  6.         string mPhrase
  7.     'methods
  8.    public
  9.         method Set( string aString )
  10.             mPhrase = aString
  11.         end method
  12.        
  13.         method Prnt()
  14.             print mPhrase
  15.         end method
  16.        
  17.         method "?"( string aString)
  18.             print aString
  19.         end method
  20.        
  21.         method "??"()
  22.             print "to " crlf "believe."
  23.         end method
  24. end class
  25.  
  26. tiny t
  27. t.Set "The X Files"
  28. t.Prnt
  29. t.? "I want "
  30. t.??

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #10 on: June 11, 2012, 02:20:16 PM »
Hi Kent,

For operator overloading you need to implement "load" "push" "pop" and "=" as the minimum operator set. The class. A static accumulator stack is also required'

The easiest way to get an operator-supporting class going is to base it on the vector example. You can also create custom operators by defining them before the class that uses them like this:

operator "aga" 9, "ogo" 8 ,...
where the number indicates the precedence level (ascending priority)
a aga b ogo (c+d)

Code: OxygenBasic
  1.  
  2. '===========================
  3. 'VECTOR ARITHMETIC OPERATORS
  4. '===========================
  5.  
  6.  
  7. type vector3d
  8.  double x,y,z
  9. end type
  10.  
  11.  
  12. class VectorOp3d
  13. '===============
  14.  
  15. has vector3d
  16.  
  17. static sys i,pp
  18. static vector3d accum[32]
  19.  
  20. macro GetOperand()
  21.   vector3d*a,*b
  22.   @a=@accum+i          'establish current stack position
  23.  if pp then
  24.     @b=@a+sizeof accum 'operand is on the stack (next position up)
  25.    pp=0
  26.   else
  27.     @b=@this           'operand is this object (not on the stack)
  28.  end if
  29. end macro
  30.  
  31. method "load"()
  32.   GetOperand
  33.   a.x=b.x
  34.   a.y=b.y
  35.   a.z=b.z
  36. end method
  37.  
  38. method "push"()
  39.   i+=sizeof accum
  40. end method
  41.  
  42. method "pop"()
  43.   pp=1 'take operand from the stack (next operation)
  44.  i-=sizeof accum
  45. end method
  46.  
  47. method "="()
  48.   GetOperand
  49.   b.x=a.x
  50.   b.y=a.y
  51.   b.z=a.z
  52. end method
  53.  
  54. method "+"()
  55.   GetOperand
  56.   a.x+=b.x
  57.   a.y+=b.y
  58.   a.z+=b.z
  59. end method
  60.  
  61. method "-"()
  62.   GetOperand
  63.   a.x-=b.x
  64.   a.y-=b.y
  65.   a.z-=b.z
  66. end method
  67.  
  68. method "*"()
  69.   GetOperand
  70.   a.x*=b.x
  71.   a.y*=b.y
  72.   a.z*=b.z
  73. end method
  74.  
  75. method "/"()
  76.   GetOperand
  77.   a.x/=b.x
  78.   a.y/=b.y
  79.   a.z/=b.z
  80. end method
  81.  
  82. end class
  83.  
  84. '#recordof vectorop3d
  85.  
  86.  
  87.  
  88. vectorop3d va[9],vb[9],vc[9]
  89.  
  90. 'assign xyz values
  91.  
  92. va<=1.0,2.0,3.0
  93. vb<=0.1, 0.2, 0.3, 0.01, 0.02, 0.03
  94.  
  95. 'test expression
  96.  
  97. 'vc=vb[1]+va*va+vb[2]
  98. vc=(vb[1])+va*va+vb[2]
  99.  
  100. print str(vc.y,4)
  101.  
  102.  
  103.  
  104.  

Charles
« Last Edit: June 11, 2012, 04:25:26 PM by Charles Pegge »

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #11 on: June 11, 2012, 02:47:54 PM »
Bstrings need explicit memory management

bstring b= nuls 100
...
frees b
'when b is no longer needed

Also low level memory allocation

sys a=getmemory 100
char z at a 'creating a char buffer in the memorry block
...
freememory a


Charles


kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #12 on: June 11, 2012, 03:15:15 PM »
Thanks. I will need to study this as it is not sinking in. Only experimenting will help. But your tips give me a focusing point. Thanks again.

Charles Pegge

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #13 on: June 11, 2012, 04:02:34 PM »
I'll cook up a sensible example with custom operators, Kent.

I've updated the listing above. I hope its meaning is a bit clearer.

The multiassign went pear-shaped on my last update (about 6 hours ago)
Sorry about that. It affects the example above.

Corrected Oxygen zip attached

New zip attached further on
« Last Edit: June 12, 2012, 06:07:56 AM by Charles Pegge »

kryton9

  • Guest
Re: Classes, Unions and Arrays Help Please
« Reply #14 on: June 11, 2012, 04:43:11 PM »
Thanks Charles, I am having fun analyzing and seeing what I can and can't do. The new commented example helps.