fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cmath>
  4.  
  5. using namespace std;
  6.  
  7. class Rectangle
  8. {
  9. int length, width;
  10. char *color; // char pointer
  11.  
  12. public:
  13. Rectangle(int x, int y, const char *str)
  14. {
  15. length = x;
  16. width = y;
  17. // allocating memory for color
  18. color = (char *)malloc(sizeof(char) * 50);
  19. strcpy(color, str);
  20. }
  21. Rectangle()
  22. {
  23. // if no parameter is given,default value
  24. length = 0;
  25. width = 0;
  26. // allocating memory for color
  27. color = (char *)malloc(sizeof(char) * 50);
  28. strcpy(color, "NONE");
  29. }
  30. ~Rectangle()
  31. {
  32. free(color);
  33. }
  34.  
  35. int getLength()
  36. {
  37. return length;
  38. }
  39. int getWidth()
  40. {
  41. return width;
  42. }
  43.  
  44. void setColor(const char *str)
  45. {
  46. free(color);
  47. color = (char *)malloc(sizeof(char) * 50);
  48.  
  49. strcpy(color, str);
  50. }
  51. int getArea()
  52. {
  53. return (length * width);
  54. }
  55. int getPerimeter()
  56. {
  57. return 2 * (length + width);
  58. }
  59. char *getColor()
  60. {
  61. return color;
  62. }
  63. void setLength(int x)
  64. {
  65. length = x;
  66. }
  67. void setWidth(int x)
  68. {
  69. width = x;
  70. }
  71. };
  72.  
  73. class Triangle
  74. {
  75. int a, b, c; // every side
  76. char *color;
  77.  
  78. public:
  79. Triangle(int x, int y, int z, const char *str)
  80. {
  81.  
  82. a = x;
  83. b = y;
  84. c = z;
  85. // allocating memory for color
  86. color = (char *)malloc(sizeof(char) * 50);
  87. strcpy(color, str);
  88. }
  89. Triangle()
  90. {
  91. // if no parameter is given,default value
  92. a = 0;
  93. b = 0;
  94. c = 0;
  95. // allocating memory for color
  96. color = (char *)malloc(sizeof(char) * 50);
  97. strcpy(color, "NONE");
  98. // none means no color
  99. }
  100. ~Triangle()
  101. {
  102. free(color);
  103. }
  104.  
  105. int getA()
  106. {
  107. return a;
  108. }
  109. int getB()
  110. {
  111. return b;
  112. }
  113. int getC()
  114. {
  115. return c;
  116. }
  117. float getArea()
  118. {
  119. int s = (a + b + c) / 2;
  120. return sqrt(s * (s - a) * (s - b) * (s - c));
  121. }
  122. int getPerimeter()
  123. {
  124. return (a + b + c);
  125. }
  126. char *getColor()
  127. {
  128. return color;
  129. }
  130. void setColor(const char *str)
  131. {
  132. free(color);
  133. color = (char *)malloc(sizeof(char) * 50);
  134. strcpy(color, str);
  135. }
  136. void setA(int x)
  137. {
  138. a = x;
  139. }
  140. void setB(int y)
  141. {
  142. b = y;
  143. }
  144. void setC(int z)
  145. {
  146. c = z;
  147. }
  148. };
  149.  
  150. class Circle
  151. {
  152. int radius;
  153. char *color;
  154.  
  155. public:
  156. Circle(int x, const char *str)
  157. {
  158. // constructor
  159. radius = x;
  160. color = (char *)malloc(sizeof(char) * 50);
  161. strcpy(color, str);
  162. }
  163. Circle()
  164. {
  165. // if no parameter is given,default value
  166. radius = 0;
  167. // allocating memory for color
  168. color = (char *)malloc(sizeof(char) * 50);
  169. strcpy(color, "NONE");
  170. }
  171. ~Circle()
  172. {
  173. free(color);
  174. }
  175.  
  176. int getRadius()
  177. {
  178. return radius;
  179. }
  180. int getPerimeter()
  181. {
  182. return (2 * 3.1416 * radius);
  183. }
  184. float getArea()
  185. {
  186. return ((3.14) * radius * radius);
  187. }
  188. char *getColor()
  189. {
  190. return color;
  191. }
  192. void setColor(const char *str)
  193. {
  194. free(color);
  195. color = (char *)malloc(sizeof(char) * 50);
  196. strcpy(color, str);
  197. }
  198. void setRadius(int x)
  199. {
  200. radius = x;
  201. }
  202. };
  203.  
  204. class ShapeCollection
  205. {
  206. int RecCount, CircleCount, TriCount;
  207. // array of 100 elements for each
  208. Rectangle r[100];
  209. Triangle t[100];
  210. Circle c[100];
  211. int i, j, k;
  212.  
  213. public:
  214. ShapeCollection()
  215. {
  216. // construction
  217. i = 0;
  218. j = 0;
  219. k = 0;
  220. RecCount = 0;
  221. TriCount = 0;
  222. CircleCount = 0;
  223. }
  224. void addRectangle(Rectangle &x)
  225. {
  226. // using setter and getter
  227. int len = x.getLength();
  228. r[i].setLength(len);
  229. int wid = x.getWidth();
  230. r[i].setWidth(wid);
  231.  
  232. i++;
  233. RecCount++;
  234. // increasing count
  235. }
  236. void addTriangle(Triangle &x)
  237. {
  238. // using setter and getter
  239.  
  240. int len1 = x.getA();
  241. t[j].setA(len1);
  242. int len2 = x.getB();
  243. t[j].setB(len2);
  244. int len3 = x.getC();
  245. t[j].setC(len3);
  246. // changing the inital value of t[j]
  247.  
  248. j++;
  249. TriCount++;
  250. // increasing count
  251. }
  252. void addCircle(Circle &x)
  253. {
  254.  
  255. int rad = x.getRadius();
  256. c[k].setRadius(rad);
  257.  
  258. k++;
  259. CircleCount++;
  260. }
  261. int getRectCount()
  262. {
  263. return RecCount;
  264. }
  265. int getTriCount()
  266. {
  267. return TriCount;
  268. }
  269. int getCircCount()
  270. {
  271. return CircleCount;
  272. }
  273. void printRectangles()
  274. {
  275. for (int i = 0; i < RecCount; i++)
  276. {
  277. cout << "Rectangle " << i << ": ";
  278. cout << "length: " << r[i].getLength() << " ";
  279. cout << "Width: " << r[i].getWidth() << endl;
  280. // printing in the desired way
  281. }
  282. }
  283.  
  284. void printTriangles()
  285. {
  286. for (int j = 0; j < TriCount; j++)
  287. {
  288. cout << "Triangle " << j << " : ";
  289. cout << "a: " << t[j].getA() << " ";
  290. cout << "b: " << t[j].getB() << " ";
  291. cout << "c: " << t[j].getC() << endl;
  292. }
  293. }
  294.  
  295. void printCircles()
  296. {
  297. for (int k = 0; k < CircleCount; k++)
  298. {
  299. cout << "Circle " << k << ": ";
  300. cout << "radius: " << c[k].getRadius() << " ";
  301. }
  302. cout << endl;
  303. }
  304. };
  305.  
  306. int main()
  307. {
  308. // Create rectangle with length, width, color
  309. Rectangle r1(10, 20, "Red");
  310. // The Color is stored using malloc, which will be freed during destruction
  311. cout << "Rectangle Perimeter: " << r1.getPerimeter() << endl;
  312. cout << "Rectangle Area: " << r1.getArea() << endl;
  313. cout << "Rectangle Color: " << r1.getColor() << endl;
  314. // When changing the color, you need to free the memory of the old color
  315. // and allocate new memory for the new color
  316. r1.setColor("Yellow");
  317. cout << "Rectangle Color: " << r1.getColor() << endl;
  318. cout << "--------------------------------------" << endl;
  319.  
  320. // Create triangle with a, b, c, color. (a, b, c are lengths of the sides)
  321. Triangle t1(3, 4, 5, "Blue");
  322. cout << "Triangle Perimeter: " << t1.getPerimeter() << endl;
  323. cout << "Triangle Color: " << t1.getColor() << endl;
  324. cout << "Triangle Area: " << t1.getArea() << endl;
  325. t1.setColor("Orange");
  326. cout << "Triangle Color: " << t1.getColor() << endl;
  327. cout << "--------------------------------------" << endl;
  328.  
  329. // Create circle with radius, color
  330. Circle c1(7, "Green");
  331. cout << "Circle Perimeter: " << c1.getPerimeter() << endl;
  332. cout << "Circle Area: " << c1.getArea() << endl;
  333. cout << "Circle Color: " << c1.getColor() << endl;
  334. c1.setColor("Purple");
  335. cout << "Circle Color: " << c1.getColor() << endl;
  336. cout << "--------------------------------------" << endl;
  337.  
  338. /*
  339.   When constructing the ShapeCollection class, you will create static arrays for 100
  340.   rectangles, triangles and circles. You don’t have to dynamically allocate memory for this.
  341.   */
  342. ShapeCollection shapes;
  343. /* IMPORTANT: You need to pass the objects by reference to the add functions
  344.   If you pass by value, the copy constructor will be called and the dynamically
  345.   allocated memory will be copied, leading to double free errors when things go
  346.   out of scope. Once passed by reference, do not directly store the reference in
  347.   the array. Instead, copy the data from the reference to the array element.
  348.   We will see better ways to handle this in the upcoming sessions.
  349.   */
  350. shapes.addRectangle(r1);
  351. shapes.addTriangle(t1);
  352. shapes.addCircle(c1);
  353.  
  354. Rectangle r2(15, 25, "Black");
  355. shapes.addRectangle(r2);
  356. Triangle t2(5, 12, 13, "White");
  357. shapes.addTriangle(t2);
  358.  
  359. cout << "Number of Rectangles: " << shapes.getRectCount() << endl;
  360. cout << "Number of Triangles: " << shapes.getTriCount() << endl;
  361. cout << "Number of Circles: " << shapes.getCircCount() << endl;
  362. cout << "--------------------------------------" << endl;
  363.  
  364. shapes.printRectangles();
  365. shapes.printTriangles();
  366. shapes.printCircles();
  367.  
  368. return 0;
  369. }
  370.  
  371.  
Success #stdin #stdout 0s 5272KB
stdin
 
stdout
Rectangle Perimeter: 60
Rectangle Area: 200
Rectangle Color: Red
Rectangle Color: Yellow
--------------------------------------
Triangle Perimeter: 12
Triangle Color: Blue
Triangle Area: 6
Triangle Color: Orange
--------------------------------------
Circle Perimeter: 43
Circle Area: 153.86
Circle Color: Green
Circle Color: Purple
--------------------------------------
Number of Rectangles: 2
Number of Triangles: 2
Number of Circles: 1
--------------------------------------
Rectangle 0: length: 10 Width: 20
Rectangle 1: length: 15 Width: 25
Triangle 0 : a: 3 b: 4 c: 5
Triangle 1 : a: 5 b: 12 c: 13
Circle 0: radius: 7