PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Examples/lua/owner/example.cxx

#
C++ | 69 lines | 53 code | 14 blank | 2 comment | 5 complexity | 8544e25cbe19bdb7e9061881b73d58aa MD5 | raw file
Possible License(s): LGPL-2.1, Cube, GPL-3.0, 0BSD, GPL-2.0
  1. /* File : example.c */
  2. #include "example.h"
  3. #include <stdio.h>
  4. #define M_PI 3.14159265358979323846
  5. /* Move the shape to a new location */
  6. void Shape::move(double dx, double dy) {
  7. x += dx;
  8. y += dy;
  9. }
  10. int Shape::nshapes = 0;
  11. double Circle::area(void) {
  12. return M_PI*radius*radius;
  13. }
  14. double Circle::perimeter(void) {
  15. return 2*M_PI*radius;
  16. }
  17. double Square::area(void) {
  18. return width*width;
  19. }
  20. double Square::perimeter(void) {
  21. return 4*width;
  22. }
  23. Circle* createCircle(double w)
  24. {
  25. return new Circle(w);
  26. }
  27. Square* createSquare(double w)
  28. {
  29. return new Square(w);
  30. }
  31. ShapeOwner::ShapeOwner() {printf(" ShapeOwner(%p)\n",this);}
  32. ShapeOwner::~ShapeOwner()
  33. {
  34. printf(" ~ShapeOwner(%p)\n",this);
  35. for(unsigned i=0;i<shapes.size();i++)
  36. delete shapes[i];
  37. }
  38. void ShapeOwner::add(Shape* ptr) // this method takes ownership of the object
  39. {
  40. shapes.push_back(ptr);
  41. }
  42. Shape* ShapeOwner::get(int idx) // this pointer is still owned by the class (assessor)
  43. {
  44. if (idx < 0 || idx >= static_cast<int>(shapes.size()))
  45. return NULL;
  46. return shapes[idx];
  47. }
  48. Shape* ShapeOwner::remove(int idx) // this method returns memory which must be deleted
  49. {
  50. if (idx < 0 || idx >= static_cast<int>(shapes.size()))
  51. return NULL;
  52. Shape* ptr=shapes[idx];
  53. shapes.erase(shapes.begin()+idx);
  54. return ptr;
  55. }