Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.14 KB | None | 0 0
  1. // Shape.cpp
  2. //
  3. // CompSci 141 / CSE 141 / Informatics 101 Fall 2009
  4. // Assignment #4
  5. //
  6. // Contains the definitions needed to emulate the Shape "class".
  7.  
  8. #include "Shape.h"
  9.  
  10.  
  11.  
  12. // This global variable is a pointer to the VMT for the Shape "class".
  13. // Since there will only be one such table for all instances of a
  14. // particular class, it makes sense to use a global variable for it.
  15. void** vmtShape;
  16.  
  17.  
  18.  
  19. // Here are the methods of the Shape class. Notice that they're
  20. // basically the same as the methods defined in the original C++
  21. // example (OOPorig), except that they have an additional parameter
  22. // called _this, which represents the object that they're being
  23. // called on.
  24.  
  25.  
  26. // This is the same constructor. Note that it's not the constructor's
  27. // job to allocate memory for an object; the constructor's job is to
  28. // take an object that's already been allocated and initialize it,
  29. // which is why you don't see the use of the "new" operator or a call
  30. // to malloc() here.
  31. //
  32. // Remember that, since Shape is abstract, there's no way to create
  33. // Shape objects. But you still need a Shape constructor, since the
  34. // constructor's of Shape's derived classes (e.g. Circle) will need
  35. // to call it.
  36. void Shape_Shape(Shape* _this, double positionX, double positionY)
  37. {
  38. _this->positionX = positionX;
  39. _this->positionY = positionY;
  40. }
  41.  
  42.  
  43. double Shape_getPositionX(Shape* _this)
  44. {
  45. return _this->positionX;
  46. }
  47.  
  48.  
  49. double Shape_getPositionY(Shape* _this)
  50. {
  51. return _this->positionY;
  52. }
  53.  
  54.  
  55. void Shape_move(Shape* _this, double positionX, double positionY)
  56. {
  57. _this->positionX = positionX;
  58. _this->positionY = positionY;
  59. }
  60.  
  61.  
  62.  
  63. // This function creates and initializes the VMT for the Shape class.
  64. // It needs to be called before the program runs. (Note that the main()
  65. // method in main.cpp first calls a function called createVMTs(), which
  66. // does just that.)
  67.  
  68. void Shape_createVMT()
  69. {
  70. vmtShape = new void*[4];
  71. vmtShape[0] = (void*) Shape_getPositionX;
  72. vmtShape[1] = (void*) Shape_getPositionY;
  73. vmtShape[2] = (void*) Shape_move;
  74.  
  75. // area() is abstract, so its entry in the VMT should be 0 (NULL).
  76. vmtShape[3] = (void*) 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement