Author Topic: class example question  (Read 1608 times)

0 Members and 1 Guest are viewing this topic.

Frankolinox

  • Guest
class example question
« on: June 30, 2014, 12:44:40 AM »
lalala :)
« Last Edit: October 01, 2014, 08:35:13 AM by Frankolinox »

pber

  • Guest
Re: class example question
« Reply #1 on: June 30, 2014, 03:04:25 AM »
Code: [Select]
class cAstroUnit
    double a
    double b
    double c
METHOD constructor(a1 AS DOUBLE)
a = a1
b = a1*149597870691
c = b/1000
END METHOD

METHOD ToString() AS STRING
return a ", " b " meters = " c " kilometers"
END METHOD
  END CLASS

cAstroUnit cA
new cAstroUnit cA 123
print "result: " ca.toString()

...this just compiles, can't guess if it is right too.
cheers

Charles Pegge

  • Guest
Re: class example question
« Reply #2 on: June 30, 2014, 08:14:37 AM »
new and del are used to create / destroy persistent objects in a C++ish  way.

When using these macros, constructor and destructor methods are expected, and the object is created from allocated memory, (like strings)

Trivial example:
Code: [Select]
class foo

bstring s
double  a,b,c

method constructor(double m)
a=1.5*m
b=2.5*m
c=3.5*m
s="name"
end method

method destructor()
a=0
b=0
c=0
frees a
end method

method sum()
return a+b+c
end method

end class

new foo FooObj(10)
print FooObj.sum '75
...
del FooObj