Most programming languages have classes. A class is a user-defined type that also has functions. To understand user-defined classes, you have to be familiar with variables, user-defined types and user-defined functions.
Note: To create new class you can use menu File -> New -> New Class. This tutorial does not use it.
Let's create a simple rectangle class. Class definition includes class name and member variables.
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
function double'width double'height Initializes the object. m_width=width m_height=height
Member function CRect.Area
function'double Calculates rectangle area. ret m_width*m_height
Member function CRect.Hypotenuse
function'double Calculates rectangle hypotenuse. 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). Example:
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"
You can create new classes that inherit member variableas and member functions of existing classes. Let's create a class that inherits from CRect.
class CColorRect :CRect m_color
Member function CColorRect.Init
function double'width double'height color this.CRect.Init(width height) m_color=color
Member function CColorRect.GetColor
function# ret m_color
CColorRect r3.Init(10 20 0xffff) out "0x%X" r3.GetColor ;;call function of CColorRect out r3.Area ;;call function inherited from CRect
By default, member variables and functions are public. They can be accessed like
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:
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 calling 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.
Member functions always are called with a variable of that type. Example:
CRect r1 r2 ... r1.Func r2.Func
If Func wants 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
out this.m_width ;;same as out m_width out this.Area ;;same as out Area out &this ;;address of the variable
A class can optionally have constructor, destructor and operator=. To add them, use item names like in the examples.
Member function CRect (the name is the same as the class name)
out "This function is called when the variable is created." m_width=1 m_height=1
Member function CRect. (the name is the same as the class name, with . at the end)
out "This function is called before destroying the variable."
Member function CRect= (the name is the same as the class name, with = at the end)
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
CRect r1 r2 r1.Init(5 5) out r1.Area out r2.Area r2=r1 out r1.Area out r2.Area