Guest User

Untitled

a guest
Nov 25th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. #include<iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. class shape
  6. {
  7. public:
  8. shape(int x=0)
  9. {
  10. ;
  11. }
  12. //function to draw shape
  13. virtual void draw() = 0;
  14.  
  15. //------function to calculate area
  16. virtual float area() = 0;
  17.  
  18. //function to return name of the shape
  19. virtual string name() = 0;
  20.  
  21. void print_area()
  22. {
  23. cout << this->area() << endl;
  24. }
  25. };
  26.  
  27. class rectangle : public shape
  28. {
  29. float length, width;
  30. public:
  31. //parameterized constructor
  32. rectangle(float length ,float width): shape(0)
  33. {
  34. this->length = length;
  35. this->width = width;
  36. }
  37. //------function to return name of the shape "Rectangle"
  38. string name() { return "Rectangle"; }
  39. //draw the rectangle
  40. void draw()
  41. {
  42. for (int i = 0; i<length; i++)
  43. {
  44. for (int j = 0; j<width; j++)
  45. {
  46. if (i == 0 || i == length - 1 || j == 0 || j == width - 1)
  47. cout << "*";
  48. else
  49. cout << " ";
  50. }
  51. cout << endl;
  52. }
  53. }
  54. //calculate area of the rectangle
  55. float area() { return length*width; }
  56.  
  57. };
  58.  
  59. class square : public rectangle
  60. {
  61. float side;
  62. public:
  63. //parameterized constructor
  64. square(float side):rectangle(side,side)
  65. {
  66. this->side=side;
  67. }
  68. //------function to return name of the shape "Square"
  69. string name() { return "Square"; }
  70. };
  71.  
  72. class rhombus :public shape
  73. {
  74. float diagonal_1, diagonal_2;
  75. public:
  76. //parameterized constructor
  77. rhombus(float diagonal_1,float diagonal_2):shape(0)
  78. {
  79. this->diagonal_1=diagonal_1;
  80. this->diagonal_2 = diagonal_2;
  81. }
  82. string name() { return "Rhombus"; }
  83. //draw the rhombus
  84. void draw()
  85. {
  86. int i = 0, j = 0,d= diagonal_1-1;
  87.  
  88. for (i = -d; i <= d; i++)
  89. {
  90. for (j = -d; j <= d; j++)
  91. {
  92. if (abs(i) + abs(j) == d)
  93. cout << "*";
  94. else
  95. cout << " ";
  96. }
  97. cout << endl;
  98. }
  99. }
  100. //calculate area of the rhombus
  101. float area(){ return (diagonal_1 * diagonal_2) / 2; }
  102. };
  103.  
  104. class vertical_line :public shape
  105. {
  106. int v_length;
  107. public:
  108. //parameterized constructor
  109. vertical_line(int v_length) :shape(0)
  110. {
  111. this->v_length= v_length;
  112. }
  113. string name() { return "Vertical line"; }
  114.  
  115. //draw the vertical line
  116. void draw()
  117. {
  118. for(int i=0;i<v_length;i++)
  119. cout << "*" << endl;
  120. }
  121. //calculate area of the vertical line
  122. float area() { return 0;}
  123. };
  124.  
  125. class horizontal_line :public shape
  126. {
  127. int h_length;
  128. public:
  129. //parameterized constructor
  130. horizontal_line(int h_length) :shape(0)
  131. {
  132. this->h_length= h_length;
  133. }
  134. string name() { return "horizontal line"; }
  135. //draw the horizontal line
  136. void draw()
  137. {
  138. for (int i = 0;i<h_length;i++)
  139. cout << "*";
  140. cout << endl;
  141. }
  142. //calculate area of the horizontal line
  143. float area() { return 0; }
  144. };
  145.  
  146.  
  147. void main(void)
  148. {
  149. float length, width, side,diagonal_1, diagonal_2;
  150. int h_length, v_length;
  151. shape* shapes[30] = { NULL };
  152. int count = 0;
  153. while (1)
  154. {
  155. cout << "Please choose from the following:" << endl << endl;
  156. cout << "1. Make a new shape" << endl << "2. List shapes" << endl <<
  157. "3. Query Shape Info" << endl << "4. Draw a shape" << endl <<
  158. "5. Exit" << endl << endl;
  159. int choice;
  160. cin >> choice;
  161. cout << endl;
  162. if (choice == 1)
  163. {
  164. while (1)
  165. {
  166. //prompt user to choose a shape
  167. cout << "Please choose from the following: " << endl << endl <<
  168. "1. Rectangle" << endl << "2. Square"<< endl
  169. << "3. Rhombus" << endl << "4. Vertical Line" << endl <<
  170. "5. Horizontal Line" << endl << "6. Back" << endl;
  171.  
  172. int ans;
  173. cin >> ans;
  174. if (ans == 1)
  175. {
  176. while (1)
  177. {
  178. //prompt user to enter length and width of the rectangle
  179. cout << "Please enter length and width of the rectangle:" << endl;
  180. cin >> length >> width;
  181. //check sides values
  182. if (length > 100 || length < 2 || width > 100 || width < 2)
  183. {
  184. cout << "Sides must be between 2 and 100\n\n";
  185. }
  186. else
  187. break;
  188. }
  189. shapes[count] = new rectangle(length,width);
  190. count++;
  191. cout << endl;
  192. }
  193. else if (ans == 2)
  194. {
  195. while (1)
  196. {
  197. //prompt user to enter side of the square
  198. cout << "Please enter a side of a square: ";
  199. cin >> side;
  200. cout << endl;
  201. //check side value
  202. if (side < 2 || side>100)
  203. {
  204. cout << "side must be between 2 and 100\n\n";
  205. }
  206. else
  207. break;
  208. }
  209. shapes[count] = new square(side);
  210. count++;
  211. cout << endl;
  212. cout << endl;
  213.  
  214. }
  215. else if (ans == 3)
  216. {
  217. while (1)
  218. {
  219. //prompt user to enter diagonals of the rhombus
  220. cout << "Please enter two diagonls of the rhombus: \n";
  221. cin >> diagonal_1 >> diagonal_2;
  222. //check diagonals values
  223. if (diagonal_1 > 100 || diagonal_1 < 2 || diagonal_2 > 100 || diagonal_2 < 2)
  224. cout << "Diagonals must be between 2 and 100" << endl;
  225. else
  226. break;
  227. }
  228. shapes[count] = new rhombus(diagonal_1, diagonal_2 );
  229. count++;
  230. cout << endl;
  231. cout << endl;
  232.  
  233. }
  234. else if (ans == 4)
  235. {
  236. while (1)
  237. {
  238. //prompt user to enter length of the vertical line
  239. cout << "Please enter length of the vertical line: \n";
  240. cin >> v_length;
  241. //check length value
  242. if (v_length > 100 || v_length < 1)
  243. cout << "must be between 1 and 100" << endl;
  244. else
  245. break;
  246.  
  247. }
  248. shapes[count] = new vertical_line(v_length);
  249. cout << endl;
  250. count++;
  251. cout << endl;
  252. cout << endl;
  253. }
  254. else if (ans == 5)
  255. {
  256. while (1)
  257. {
  258. //prompt user to enter length of the horizontal line
  259. cout << "Please enter length of the horizontal line: \n";
  260. cin >> h_length;
  261. //check length value
  262. if (h_length > 100 || h_length < 1)
  263. cout << "must be between 1 and 100" << endl;
  264. else
  265. break;
  266. }
  267. shapes[count] = new horizontal_line(h_length);
  268. cout << endl;
  269. count++;
  270. cout << endl;
  271. cout << endl;
  272. }
  273. else if (ans == 6)
  274. {
  275. break;
  276. }
  277.  
  278. else
  279. {
  280. cout << endl << "Invalid choice" << endl << endl;
  281. continue;
  282. }
  283. }
  284. }
  285. if(choice ==2)
  286. {
  287. if (count == 0)
  288. {
  289. cout << "Shapes not found" << endl <<
  290. "Please make new shapes" << endl << endl;
  291. }
  292. else
  293. {
  294. for (int i = 0;i < count;i++)
  295. {
  296. cout << i+1<<". "<<shapes[i]->name() << endl;
  297. }
  298. cout << endl;
  299. }
  300. }
  301. if (choice == 3)
  302. {
  303. if (count == 0)
  304. {
  305. cout << "Shapes not found" << endl <<
  306. "Please make new shapes" << endl << endl;
  307. }
  308.  
  309. else
  310. {
  311. for (int i = 0;i < count;i++)
  312. {
  313. cout << shapes[i]->name() << endl <<"Area: ";
  314. shapes[i]->print_area();
  315. cout << endl;
  316. }
  317. cout << endl;
  318. }
  319. }
  320. if(choice==4)
  321. {
  322. if (count == 0)
  323. {
  324. cout << "Shapes not found" << endl <<
  325. "Please make new shapes" << endl << endl;
  326. }
  327. else
  328. {
  329. for (int i = 0;i < count;i++)
  330. {
  331. cout << shapes[i]->name() << endl << endl;
  332. shapes[i]->draw();
  333. cout << endl;
  334. }
  335. }
  336. }
  337. if (choice == 5)
  338. {
  339. for (int i = 0;i < 30;i++)
  340. delete(shapes[i]);
  341. exit(1);
  342. }
  343. }
  344. }
Add Comment
Please, Sign In to add comment