runme.pl 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. # file: runme.pl
  2. # This file illustrates the low-level C++ interface
  3. # created by SWIG. In this case, all of our C++ classes
  4. # get converted into function calls.
  5. use example;
  6. # ----- Object creation -----
  7. print "Creating some objects:\n";
  8. $c = examplec::new_Circle(10);
  9. print " Created circle $c\n";
  10. $s = examplec::new_Square(10);
  11. print " Created square $s\n";
  12. # ----- Access a static member -----
  13. print "\nA total of $examplec::Shape_nshapes shapes were created\n";
  14. # ----- Member data access -----
  15. # Set the location of the object.
  16. # Note: methods in the base class Shape are used since
  17. # x and y are defined there.
  18. examplec::Shape_x_set($c, 20);
  19. examplec::Shape_y_set($c, 30);
  20. examplec::Shape_x_set($s,-10);
  21. examplec::Shape_y_set($s,5);
  22. print "\nHere is their current position:\n";
  23. print " Circle = (",examplec::Shape_x_get($c),",", examplec::Shape_y_get($c),")\n";
  24. print " Square = (",examplec::Shape_x_get($s),",", examplec::Shape_y_get($s),")\n";
  25. # ----- Call some methods -----
  26. print "\nHere are some properties of the shapes:\n";
  27. foreach $o ($c,$s) {
  28. print " $o\n";
  29. print " area = ", examplec::Shape_area($o), "\n";
  30. print " perimeter = ", examplec::Shape_perimeter($o), "\n";
  31. }
  32. # Notice how the Shape_area() and Shape_perimeter() functions really
  33. # invoke the appropriate virtual method on each object.
  34. # ----- Delete everything -----
  35. print "\nGuess I'll clean up now\n";
  36. # Note: this invokes the virtual destructor
  37. examplec::delete_Shape($c);
  38. examplec::delete_Shape($s);
  39. print $examplec::Shape_nshapes," shapes remain\n";
  40. print "Goodbye\n";