Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes
#1
Could I get an example or two on user defined classes. I am tryitng to make heads and tails of the help file and not getting too far.

Thanks.
#2
The Classes topic in QM help is for those that already know what is a class. Most programming languages have classes.

To understand user-defined classes, you have to be familiar with variables, user-defined types and user-defined functions. A class is an user-defined type that also has functions.

Let's create a simple rectangle class. Class definition includes class name and member variables.

Macro
Code:
Copy      Help
class CRect
,double'm_width
,double'm_height
Put it in some macro and compile or run the macro. It lets QM know about the new class. To make it always available, put it in a function that runs at startup. For example, in init2 (create it if does not exist).

Now create 3 member functions. To add member functions use menu File -> New -> New Member Function. The item name consists of class name, . and function name.

Member function CRect.Init
Code:
Copy      Help
function double'width double'height

;Initializes the variable.


m_width=width
m_height=height

Member function CRect.Area
Code:
Copy      Help
function'double

;Calculates rectangle area.


ret m_width*m_height

Member function CRect.Hypotenuse
Code:
Copy      Help
function'double

;Calculates rectangle hypothenuse.
;It is distance between two opposite corners.


ret _hypot(m_width m_height)

Now the class is created and you can use it anywhere. Declare variables of CRect type and call functions using syntax variable.Function(arguments).

Macro
Code:
Copy      Help
CRect r.Init(10 20)
out r.Area
out r.Hypotenuse

CRect r2.Init(11 19)
if(r2.Area>r.Area) out "r2 is bigger"
else out "r is bigger"
#3
You can create new classes that inherit member variableas and member functions of existing classes. Let's create a class that inherits from CRect.

Macro
Code:
Copy      Help
class CColorRect :CRect
,m_color

Member function CColorRect.Init
Code:
Copy      Help
function double'width double'height color

this.CRect.Init(width height)
m_color=color

Member function CColorRect.GetColor
Code:
Copy      Help
function#

ret m_color

Example:

Macro
Code:
Copy      Help
CColorRect r3.Init(10 20 0xffff)
out "0x%X" r3.GetColor ;;call function of CColorRect
out r3.Area ;;call function inherited from CRect
#4
By default, member variables and functions are public. They can be accessed like

Macro
Code:
Copy      Help
CRect r
r.m_width=15

To protect member variables from accessing from outside of the class, in class definition add one or two hyphens before these members:

Macro
Code:
Copy      Help
class CRect
,-double'm_width ;;m_width is protected. It can be used only in functions of CRect and inherited classes (eg CColorRect).
,--double'm_height ;;m_height is private. It can be used only in functions of CRect class.

Now r.m_width=15 would generate error.

You also can protect some member functions from callig from outside the class. You can do it in function's Properties dialog.

To hide member variables and functions without protecting, let the variable/function name begin with __. Examples: __m_hidden, CRect.__Hidden. Or place the functions in a folder that has 'private functions' checked in Folder Properties dialog.
#5
Member functions always are called with a variable of that type. Example:

Macro
Code:
Copy      Help
CRect r1 r2
;...
r1.Func
r2.Func

If Func want to access the variable (r1, r2 or other), it can use this. It is a reference to the variable for which the function called. Example:

Member function CRect.Func
Code:
Copy      Help
out this.m_width ;;same as out m_width
out this.Area ;;same as out Area
out &this ;;address of the variable
#6
A class can optionally have constructor, destructor and operator=. To add them, use item names like in the examples.

Constructor
Member function CRect
Code:
Copy      Help
out "This function is called when the variable is created."

m_width=1
m_height=1

Destructor
Member function CRect.
Code:
Copy      Help
out "This function is called before destroying the variable."

Operator =
Member function CRect=
Code:
Copy      Help
function CRect&source

out "This function is called when you assigh one variable to another variable, both of CRect type."

this.m_width=source.m_width
this.m_height=source.m_height


Test:
Macro
Code:
Copy      Help
CRect r1 r2
r1.Init(5 5)
out r1.Area
out r2.Area
r2=r1
out r1.Area
out r2.Area
#7
Thanks for all the information. I've only been able to skim over it so far, but it looks like you did a very good job explaining everything I wanted to know. Thanks a bunch!
#8
Could you give an example of how to create a new member function for a str variable?

str s.MemberFunction("data")

out s

where the member function formats the data and assigns it to str s.

Thanks,
jimmy Vig
#9
Inside the function use this as variable name.

function $data
this.from(data "more data")
#10
this...totally right on.

str s="800.32"
s.SecondsDateTime(s)
out s

Member function str.SecondsDateTime
Code:
Copy      Help
function $data
str s=TimeSpanToStr(TimeSpanFromStr(data) 2)
this.from(s)

Totally stoked about knowing how to do this. Can definitely speed up and clean up coding! I've never been able to get classes to work right! Now I see what I was doing wrong Smile

Thank you so much!
Jimmy Vig
#11
function $data
this=TimeSpanToStr(TimeSpanFromStr(data) 2)


Forum Jump:


Users browsing this thread: 1 Guest(s)