Guest User

Untitled

a guest
Jun 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #include <string.h>
  2. #include <assert.h>
  3. #include <iostream.h>
  4.  
  5. typedef double Coord;
  6.  
  7. /*
  8. The type of X/Y points on the screen.
  9. */
  10.  
  11. enum Color {Co_red, Co_green, Co_blue};
  12.  
  13. /*
  14. Colors.
  15. */
  16.  
  17. // abstract base class for all shape types
  18. class Shape {
  19. protected:
  20. Coord xorig; // X origin
  21. Coord yorig; // Y origin
  22. Color co; // color
  23.  
  24. /*
  25. These are protected so that they can be accessed
  26. by derived classes. Private wouldn't allow this.
  27.  
  28. These data members are common to all shape types.
  29. */
  30.  
  31. public:
  32. Shape(Coord x, Coord y, Color c) :
  33. xorig(x), yorig(y), co(c) {} // constructor
  34.  
  35. /*
  36. Constructor to initialize data members common to
  37. all shape types.
  38. */
  39.  
  40. virtual ~Shape() {} // virtual destructor
  41.  
  42. /*
  43. Destructor for Shape. It's a virtual function.
  44. Destructors in derived classes are virtual also
  45. because this one is declared so.
  46. */
  47.  
  48. virtual void draw() = 0; // pure virtual draw() function
  49.  
  50. /*
  51. Similarly for the draw() function. It's a pure virtual and
  52. is not called directly.
  53. */
  54.  
  55. };
  56.  
  57. // line with X,Y destination
  58. class Line : public Shape {
  59.  
  60. /*
  61. Line is derived from Shape, and picks up its
  62. data members.
  63. */
  64.  
  65. Coord xdest; // X destination
  66. Coord ydest; // Y destination
  67.  
  68. /*
  69. Additional data members needed only for Lines.
  70. */
  71.  
  72. public:
  73. Line(Coord x, Coord y, Color c, Coord xd, Coord yd) :
  74. xdest(xd), ydest(yd),
  75. Shape(x, y, c) {} // constructor with base initialization
  76.  
  77. /*
  78. Construct a Line, calling the Shape constructor as well
  79. to initialize data members of the base class.
  80. */
  81.  
  82. ~Line() {cout << "~Line\n";} // virtual destructor
  83.  
  84. /*
  85. Destructor.
  86. */
  87.  
  88. void draw() // virtual draw function
  89. {
  90. cout << "Line" << "(";
  91. cout << xorig << ", " << yorig << ", " << int(co);
  92. cout << ", " << xdest << ", " << ydest;
  93. cout << ")\n";
  94. }
  95.  
  96. /*
  97. Draw a line.
  98. */
  99.  
  100. };
  101.  
  102. // circle with radius
  103. class Circle : public Shape {
  104. Coord rad; // radius of circle
  105.  
  106. /*
  107. Radius of circle.
  108. */
  109.  
  110. public:
  111. Circle(Coord x, Coord y, Color c, Coord r) : rad(r),
  112. Shape(x, y, c) {} // constructor with base initialization
  113.  
  114. ~Circle() {cout << "~Circle\n";} // virtual destructor
  115. void draw() // virtual draw function
  116. {
  117. cout << "Circle" << "(";
  118. cout << xorig << ", " << yorig << ", " << int(co);
  119. cout << ", " << rad;
  120. cout << ")\n";
  121. }
  122. };
  123.  
  124. // text with characters given
  125. class Text : public Shape {
  126. char* str; // copy of string
  127. public:
  128. Text(Coord x, Coord y, Color c, const char* s) :
  129. Shape(x, y, c) // constructor with base initialization
  130. {
  131. str = new char[strlen(s) + 1];
  132. assert(str);
  133. strcpy(str, s);
  134.  
  135. /*
  136. Copy out text string. Note that this would be done differently
  137. if we were taking advantage of some newer C++ features like
  138. exceptions and strings.
  139. */
  140.  
  141. }
  142. ~Text() {delete [] str; cout << "~Text\n";} // virtual dtor
  143.  
  144. /*
  145. Destructor; delete text string.
  146. */
  147.  
  148. void draw() // virtual draw function
  149. {
  150. cout << "Text" << "(";
  151. cout << xorig << ", " << yorig << ", " << int(co);
  152. cout << ", " << str;
  153. cout << ")\n";
  154. }
  155. };
  156.  
  157. int main()
  158. {
  159. const int N = 5;
  160. int i;
  161. Shape* sptrs[N];
  162.  
  163. /*
  164. Pointer to vector of Shape* pointers. Pointers to classes
  165. derived from Shape can be assigned to Shape* pointers.
  166. */
  167.  
  168. // initialize set of Shape object pointers
  169.  
  170. sptrs[0] = new Line(0.1, 0.1, Co_blue, 0.4, 0.5);
  171. sptrs[1] = new Line(0.3, 0.2, Co_red, 0.9, 0.75);
  172. sptrs[2] = new Circle(0.5, 0.5, Co_green, 0.3);
  173. sptrs[3] = new Text(0.7, 0.4, Co_blue, "Howdy!");
  174. sptrs[4] = new Circle(0.3, 0.3, Co_red, 0.1);
  175.  
  176. /*
  177. Create some shape objects.
  178. */
  179.  
  180. // draw set of shape objects
  181.  
  182. for (i = 0; i < N; i++)
  183. sptrs[i]->draw();
  184.  
  185. /*
  186. Draw them using virtual functions to pick up the
  187. right draw() function based on the actual object
  188. type being pointed at.
  189. */
  190.  
  191. // cleanup
  192.  
  193. for (i = 0; i < N; i++)
  194. delete sptrs[i];
  195.  
  196. /*
  197. Clean up the objects using virtual destructors.
  198. */
  199.  
  200. return 0;
  201. }
Add Comment
Please, Sign In to add comment