member functions of TYPE

Previous topic - Next topic

sf-in-sf

Here is an example of encapsulted functions inside a TYPE. Keep in mind they are supposed to be the external "public buttons" of your objects.
It works like that.
I didn't get the green "this" to work -both in GLb and C- but appearently "self" would be the right word. I will try it.
Code (glbasic) Select

// --------------------------------- //
// Project: encaps_function
// Start: Thursday, August 22, 2013
// IDE Version: 10.283 // just updated

// The green "this" remains a mystery to me,
// but the program works like that:
TYPE sq
size%=100;
dir%=1;
//dummy%=0
FUNCTION changesize: // a member function, "encapsulated".
INLINE //Now use the different C syntax:

if (dir>0)
{INC(size); //should be this.size?
if (size>222)
{size=200; dir=0;}
}
else
{DEC( size);
if (size<77)
{size=88 ; dir=2;}
}
// ENDIF // not in C.
ENDINLINE

ENDFUNCTION
FUNCTION draw:
INLINE
DRAWRECT( 33,33,size,size, 0xffbb99);
ENDINLINE
ENDFUNCTION
ENDTYPE

LOCAL s AS sq  //instanciation
WHILE TRUE
// call member functions of the object:
s.changesize()
s.draw()
// i.e. push the external buttons of
// the closed object.
SHOWSCREEN
WEND

// P.S. a typical example of such member functions are
// "accessors", used to get or set the encapsulated values
// of the object from the outside.
On the day the atom is a cube I will start believing in the square pixel.

kanonet

I dont really unterstad the purpose of this tutorial, since it does not really show anything. Yes the command to access the type inside his own functions is "self.". If you would show this, it may be useful for someone. Sorry, its not meaned as an offence.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

Kanonet, I think like aproach to C++ goes fine, for me it's good see codes like this... My C++ skills are very low and I think I want to do something like this the another day whitout succes... I'm sure for you, like a good porgrammer this it's very easy , but for people like me , we don't see the code very clear sometimes... like teaching works good at least for me.

kanonet

But for this example INLINE was just not neccessary, "self." would completly do the job. BTW self also works in INLINE, or you could use "this->". His post was more a question then a tutorial, thats why i answered here, IMHO it was a bit to few information get called "tutorial". But thats my opinion. ;)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

mentalthink

Sorry then I read bad...   :rant: :-[ :P

hardyx

#5
In C++, you can use "size" (the variable) or this->size, but "this->" isn't mandatory. "this" is a pointer to the type in C++.
In GLBASIC the sentence is self.size, and it's mandatory to access type vars. "self" is a reference to the type in GLB.

P.D. You can write your type functions in GLBasic code too.

sf-in-sf

#6
Thanks for the comments everyone. Someone more experienced is welcome to re-write the code in a better way, and post it to the community. Thanks in advance.
I find this tuto is an important key to get started with the  object functionality of GLb, and I feel it should appear in a form or another in the help files / manual, Gernot. It's a pitty that "self" is not documented.
On the day the atom is a cube I will start believing in the square pixel.

kanonet

I think good examples how to use Types + Functions are:
www.glbasic.com/forum/index.php?topic=4486
www.glbasic.com/forum/index.php?topic=7280
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

sf-in-sf

Nice one Hatonastick!
I'm back with a final update that only uses GLb syntax. Getting started becomes easy now.
Code (glbasic) Select
// --------------------------------- //
// Project: encaps_function

// The green "this" remains a mystery to me,
// but this is the way to stick to the GLb syntax:
TYPE sq
loc%=33; size%=100;
dir%=1;
//wish$="go_sailing"
FUNCTION changesize: // a member function, "encapsulated".

IF self.dir>0
INC self.size
IF self.size>222
self.size=200
self.dir=0
ENDIF
ELSE
DEC self.size
IF self.size<77
self.size=88 ; self.dir=2
ENDIF
ENDIF

ENDFUNCTION
FUNCTION draw:
DRAWRECT self.loc,self.loc,self.size,self.size, 0xffbb99
ENDFUNCTION
ENDTYPE

LOCAL s AS sq  //instanciation, builds object s from type/template sq.
// (s.loc=33 automatically)
LOCAL s2 AS sq
s2.loc=222 //adjustment, kind of polymorphism.
WHILE TRUE
// call member functions of the object:
s.changesize() // 'self' will refer to s.
s.draw()
s2.changesize() // 'self' will refer to s2.
s2.draw()
// i.e. push the external buttons of
// the closed object.
SHOWSCREEN
WEND

// P.S. a typical example of such member functions are
// "accessors", used to get or set the encapsulatd values
// of the object from the outside.
On the day the atom is a cube I will start believing in the square pixel.

hardyx

Better code and more readable now.

Hemlos

Yes this would be self in glbasic
ps. not a bump, i think you were in the wrong area when you started this thread, its ok ill move it to our GLBasic - en



Bing ChatGpt is pretty smart :O

MrPlow

What i would like to know is .... is it faster to use "in type functions" and how they could benefit array types?

So far I have never used these as I am unsure of the performance considerations...
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

MrTAToad

Continual use of functions in types does work out quicker than using standard functions.

In addition, keeping the program running from inside an extended type does also have a slight speed advantage.

MrPlow

Thanks MrT,

Might look into it more...for future projects...
Comp:
Speccy-48k, Speccy-128k, Amigas, PCs

MrTAToad