PageRenderTime 49ms CodeModel.GetById 1ms RepoModel.GetById 0ms 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
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  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.
  3. module runme;
  4. import tango.io.Stdout;
  5. import example;
  6. void main() {
  7. // ----- Object creation -----
  8. Stdout( "Creating some objects:" ).newline;
  9. {
  10. scope Square s = new Square(10);
  11. scope Circle c = new Circle(10);
  12. // ----- Access a static member -----
  13. Stdout.format( "{} shapes were created.", Shape.nshapes ).newline;
  14. // ----- Member data access -----
  15. // Notice how we can do this using functions specific to
  16. // the 'Circle' class.
  17. c.x = 20;
  18. c.y = 30;
  19. // Now use the same functions in the base class
  20. Shape shape = s;
  21. shape.x = -10;
  22. shape.y = 5;
  23. Stdout( "\nHere is their current position:" ).newline;
  24. Stdout.format( " Circle = ( {}, {} )", c.x, c.y ).newline;
  25. Stdout.format( " Square = ( {}, {} )", s.x, s.y ).newline;
  26. // ----- Call some methods -----
  27. Stdout( "\nHere are some properties of the shapes:" ).newline;
  28. Shape[] shapes = [ cast(Shape) c, cast(Shape) s ];
  29. foreach ( currentShape; shapes )
  30. {
  31. Stdout.format( " {}", currentShape.classinfo.name ).newline;
  32. Stdout.format( " area = {}", currentShape.area() ).newline;
  33. Stdout.format( " perimeter = {}", currentShape.perimeter() ).newline;
  34. }
  35. // Notice how the area() and perimeter() functions really
  36. // invoke the appropriate virtual method on each object.
  37. // ----- Delete everything -----
  38. Stdout( "\nGuess I'll clean up now:" ).newline;
  39. // Note: when this using scope is exited the D destructors are called which
  40. // in turn call the C++ destructors.
  41. }
  42. Stdout.format( "{} shapes remain", Shape.nshapes ).newline;
  43. Stdout( "\nGoodbye!" ).newline;
  44. }