PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/trunk/Examples/d/class/d1/runme.d

#
D | 58 lines | 30 code | 14 blank | 14 comment | 0 complexity | bc9618b7f36dbcfa535334b0f5c1aea8 MD5 | raw file
 1// This example illustrates how C++ classes can be used from D using SWIG.
 2// The D class gets mapped onto the C++ class and behaves as if it is a D class.
 3module runme;
 4
 5import tango.io.Stdout;
 6import example;
 7
 8void main() {
 9  // ----- Object creation -----
10
11  Stdout( "Creating some objects:" ).newline;
12
13  {
14    scope Square s = new Square(10);
15    scope Circle c = new Circle(10);
16
17    // ----- Access a static member -----
18    Stdout.format( "{} shapes were created.", Shape.nshapes ).newline;
19
20    // ----- Member data access -----
21
22    // Notice how we can do this using functions specific to
23    // the 'Circle' class.
24    c.x = 20;
25    c.y = 30;
26
27    // Now use the same functions in the base class
28    Shape shape = s;
29    shape.x = -10;
30    shape.y = 5;
31
32    Stdout( "\nHere is their current position:" ).newline;
33    Stdout.format( "    Circle = ( {}, {} )", c.x, c.y ).newline;
34    Stdout.format( "    Square = ( {}, {} )", s.x, s.y ).newline;
35
36    // ----- Call some methods -----
37
38    Stdout( "\nHere are some properties of the shapes:" ).newline;
39    Shape[] shapes = [ cast(Shape) c, cast(Shape) s ];
40    foreach ( currentShape; shapes )
41    {
42      Stdout.format( "    {}", currentShape.classinfo.name ).newline;
43      Stdout.format( "        area      = {}", currentShape.area() ).newline;
44      Stdout.format( "        perimeter = {}", currentShape.perimeter() ).newline;
45    }
46
47    // Notice how the area() and perimeter() functions really
48    // invoke the appropriate virtual method on each object.
49
50    // ----- Delete everything -----
51    Stdout( "\nGuess I'll clean up now:" ).newline;
52    // Note: when this using scope is exited the D destructors are called which
53    // in turn call the C++ destructors.
54  }
55
56  Stdout.format( "{} shapes remain", Shape.nshapes ).newline;
57  Stdout( "\nGoodbye!" ).newline;
58}