Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Classes
#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"


Messages In This Thread

Forum Jump:


Users browsing this thread: 6 Guest(s)