Author Topic: class understanding  (Read 2404 times)

0 Members and 1 Guest are viewing this topic.

pber

  • Guest
class understanding
« on: June 30, 2014, 03:31:09 AM »
I know I'll be happy if someone will enlight me
about two semanthics around classes.

Code: [Select]
method add(sys n,= double x, y)
and

Code: [Select]
/\
...for the latter I'm not too optimistic.
Thanks

Peter

  • Guest
Re: class understanding
« Reply #1 on: June 30, 2014, 05:12:07 AM »
Hello Frank,

Code: [Select]
class cAstroUnit
    double a
    double b
  METHOD constructor(a1 AS DOUBLE)
    a = a1
    b = a1*149597870691/1000
END METHOD

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

cAstroUnit cA

cA.constructor = 123
print "result: " cA.ToString()

pber

  • Guest
Re: class understanding
« Reply #2 on: June 30, 2014, 05:23:33 AM »
Hello Peter  ::)

Charles Pegge

  • Guest
Re: class understanding
« Reply #3 on: June 30, 2014, 08:39:37 AM »
Yes, that illustrates the simple use of a class.

/\ is low-level semantics, mostly for internal use. It separates the static components from the dynamic components in the Class type descriptor. For example:

Code: [Select]
class foo

static double sd1,sd2,sd3

double d1,d2,d3

method m1(double m) as double
return (sd1+sd2+sd3)*m
end method

method m2(double m) as double
return (d1+d2+d3)*m
end method

end class

foo fooObj

print FooObj.m1(10)

The compiler will expand the above class definition to a class type definition and a group of procedures:

Code: [Select]
class foo
static double sd1,sd2,sd3
method m1(double m) as double
method m2(double m) as double
/\
double d1,d2,d3
end class

methods of foo
  method m1(double m) as double
  return (sd1+sd2+sd3)*m
  end method
  '
  method m2(double m) as double
  return (d1+d2+d3)*m
  end method
end methods

This syntax is available to the programmer but seldom necessary :)

Charles Pegge

  • Guest
Re: class understanding
« Reply #4 on: June 30, 2014, 08:49:14 AM »
Quote
method add(sys n,= double x, y)

This syntax is available for invoking pseudo-variables. It is not essential but requires the programmer to call the method with pseudo-variable syntax.

Code: [Select]
class foo

double v[100]

method sum(int n,= double a,b,c)
v[n]=a+b+c
end method

method sum(int n) as double
return v[n]
end method

end class

foo j

j.sum(4)=1.5, 2.5, 3.5

print j.sum(4) '7.5

Without enforced pseudo-variable syntax:

Code: [Select]
class foo

double v[100]

method sum(int n, double a,b,c)
v[n]=a+b+c
end method

method sum(int n) as double
return v[n]
end method

end class

foo j

j.sum(4)=1.5, 2.5, 3.5 'accepted

j.sum(4,1.5, 2.5, 3.5) 'also accepted



print j.sum(4) '7.5
« Last Edit: June 30, 2014, 09:12:06 AM by Charles Pegge »

Frankolinox

  • Guest
Re: class understanding
« Reply #5 on: June 30, 2014, 09:19:18 AM »
thanks peter for your help :) I am not very fit with class constructions so I am learning yet

here's my final solution for my task...

Code: [Select]
' correct :) --------------------------------- //
class cAstroUnit2
    double a
    double b
    double c
        METHOD constructor(a1 AS DOUBLE)
            a = a1
            'b = a1*149597870691/1000
            b = a1*149597870691
            c = b/1000
        END METHOD
        METHOD ToString() AS STRING
            'return a ", " b " meters = " b " kilometers"
            return str(a)+", "+str(b) +" meters = " +str(c) +" kilometers"
        END METHOD
  END CLASS
cAstroUnit2 cA
cA.constructor = 1 '2
print "result: " cA.ToString() 

thanks charles too for further explanations.

thanks, frank

pber

  • Guest
Re: class understanding
« Reply #6 on: July 01, 2014, 04:08:34 AM »
Thanks Charles,

at the beginning I misunderstood how methods get defined,
it seemed to me you can re-define a single method of an existing class.
No i realize you could redefine all methods as a whole
(if
Code: [Select]
/\ +
Code: [Select]
methods of are used).
But I guess this is not the way you thought class methods.
« Last Edit: July 01, 2014, 04:28:43 AM by paolo bernardi »