Guest User

Untitled

a guest
Jun 24th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. #include <iostream.h>
  2. #include <conio.h>
  3. #include <typeinfo>
  4. class point
  5. {
  6. protected:
  7. int x,y;
  8. public:
  9. point()
  10. {
  11. x=0;
  12. y=0;
  13. }
  14. void getpoint()
  15. {
  16. cout<<"\nEnter the coordinates:";
  17. cin>>x;
  18. cin>>y;
  19. }
  20. void displaypoint()
  21. {
  22. cout<<"\nCoordinates are ("<<x<<","<<y<<")";
  23. }
  24. };
  25. class shape: public point
  26. {
  27. protected:
  28. float vertices,area,perimeter ;
  29. public:
  30. void getdetails()
  31. {
  32. getpoint();
  33. }
  34. void virtual get(){}
  35. void virtual fnarea(){}
  36. void virtual fnperimeter(){}
  37. void displayarea()
  38. {
  39. cout<<"\nArea="<<area;
  40. }
  41. void displayperi()
  42. {
  43. cout<<"\nPerimeter="<<perimeter;
  44. }
  45. };
  46. class square: public shape
  47. {
  48. float side;
  49. void get()
  50. {
  51. cout<<"\n the square object has been chosen randomly";
  52. cout<<"Enter the length of the side of the square: ";
  53. cin>>side;
  54. }
  55. void fnarea()
  56. {
  57. area=side*side;
  58. }
  59. void fnperimeter()
  60. {
  61. perimeter=4*side;
  62. }
  63. };
  64. class rectangle:public shape
  65. {
  66. float l,b;
  67. public:
  68. void get()
  69. {
  70. cout<<"\n the rectangle object has been chosen randomly";
  71. cout<<"\nEnter the length:";
  72. cin>>l;
  73. cout<<"\nEnter the breadth:";
  74. cin>>b;
  75. }
  76. void fnarea()
  77. {
  78. area=l*b;
  79. }
  80. void fnperimeter()
  81. {
  82. perimeter=2*(l+b);
  83. }
  84. };
  85. class circle:public shape
  86. {
  87. float radius;
  88. public:
  89. void get()
  90. {
  91. cout<<"\n the circle object has been chosen randomly";
  92. cout<<"\nEnter the Radius:";
  93. cin>>radius;
  94. }
  95. void fnarea()
  96. {
  97. area=3.14*radius*radius;
  98. }
  99. void fnperimeter()
  100. {
  101. perimeter=2*3.14*radius;
  102. }
  103. };
  104. class triangle: public shape
  105. {
  106. float base,height,b,c;
  107. public:
  108. void get()
  109. {
  110. cout<<"\n the triangle object has been chosen randomly";
  111. cout<<"\nEnter the base:";
  112. cin>>base;
  113. cout<<"\nEnter the height:";
  114. cin>>height;
  115. cout<<"\nEnter the other two sides:";
  116. cin>>b>>c;
  117. }
  118. void fnarea()
  119. {
  120. area=1/2*(base*height);
  121. }
  122. void fnperimeter()
  123. {
  124. perimeter=base+b+c;
  125. }
  126. };
  127. /*class ellipse:public shape
  128. {
  129. float majoraxis,minoraxis;
  130. public:
  131. void get()
  132. {
  133. cout<<"\nEnter the major axis";
  134. cin>>majoraxis;
  135. cout<<"\nEnter the minor axis";
  136. cin>>minoraxis;
  137. }
  138. void fnarea()
  139. {
  140. area=3.14*majoraxis*minoraxis;
  141. }
  142. void fnperimeter()
  143. {
  144. perimeter=(majoraxis*majoraxis+minoraxis*minoraxis)/2;
  145. }
  146. };
  147. class polygon:public shape
  148. {
  149. int no;
  150. float length;
  151. public:
  152. void get()
  153. {
  154. cout<<"\nEnter the no of vertices";
  155. cin>>no;
  156. cout<<"\nEnter the length of the side";
  157. cin>>length;
  158. }
  159. void fnperimeter()
  160. {
  161. perimeter=no*length;
  162. }
  163. };*/
  164. int main()
  165. {
  166. cout<<"\n A number will be chosen at random and as per the random digit the details of either a\n square, a rectangle, a triangle or a circle will be printed";
  167. int ch,loop,r;
  168. loop=0;
  169. shape *ptr;
  170. square sq;
  171. rectangle rect;
  172. triangle tri;
  173. circle cir;
  174. /*ellipse ell;
  175. polygon poly;*/
  176. while(loop!=1)
  177. {
  178. /*cout<<"\n1.Square\n2.Rectangle\n3.Circle\n4.Triangle\n5.Exit";
  179. cout<<"\nEnter your choice:";
  180.  
  181. cin>>ch;*/
  182. ch=(rand()%7)+1;
  183. switch(ch)
  184. {
  185. case 1:
  186. ptr=&sq;
  187. break;
  188. case 2:
  189. ptr=&rect;
  190. break;
  191. case 3:
  192. ptr=&cir;
  193. break;
  194. /*case 4:
  195. ptr=&ell;
  196. break;*/
  197. case 4:
  198. ptr=&tri;
  199. break;
  200. /*case 5:
  201. ptr=&poly;
  202. break;*/
  203. case 5:
  204. loop=1;
  205. break;
  206. /*default:
  207. cout<<"\nwrong choice";*/
  208. }
  209. /* if(typeid(*ptr)==typeid(poly))
  210. {
  211. ptr->getdetails();
  212. ptr->get();
  213. ptr->fnperimeter();
  214. ptr->displayperi();
  215. }
  216. else
  217. {*/
  218. ptr->getdetails();
  219. ptr->get();
  220. ptr->fnarea();
  221. ptr->fnperimeter();
  222. ptr->displayarea();
  223. ptr->displayperi();
  224. //}
  225. }
  226. return 0;
  227. }
Add Comment
Please, Sign In to add comment