Debian Codegraph @

examples/class/ctors-dtor.ck

raw
1 // example: defining basic class constructors + destructor
2
3 // a class
4 class Foo
5 {
6 // a member variable
7 1 => int num;
8
9 // constructor "default"
10 fun @construct() { 2 => num; }
11
12 // another constructor
13 // (using class name OR @construct is interchaneable)
14 fun Foo( int x ) { x => num; }
15
16 // yet another constructor
17 fun @construct( int x, int y ) { x*y => num; }
18
19 // yet another constructor
20 fun Foo( int x, int y, int z ) { x*y*z => num; }
21
22 // destructor
23 fun @destruct() { <<< "destructor:", this.num >>>; }
24 }
25
26 // constructor "default"
27 Foo f0;
28 // constructor "default"
29 Foo f1();
30 // another constructor
31 Foo f2(15);
32 // yet another constructor
33 new Foo(8,9) @=> Foo @ f3;
34 // yet another constructor
35 new Foo(10,11,12) @=> Foo @ f4;
36 // print
37 <<< f0.num, f1.num, f2.num, f3.num, f4.num >>>;