Guest User

Untitled

a guest
Feb 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. #include<iostream> //pgm on class polygon
  2. using namespace std;
  3. class polygon
  4. {
  5. protected:
  6. float width, height;
  7. public:
  8. void input()
  9. {
  10. cout<<"\nEnter the height and width: ";
  11. cin>>height>>width;
  12. }
  13. virtual float area()=0;
  14. };
  15. class rectangle: public polygon
  16. {
  17. public:
  18.  
  19. float area()
  20. {
  21. input();
  22. return width*height;
  23. }
  24. };
  25. class triangle:public polygon
  26. {
  27. public:
  28. //input();
  29. float area()
  30. {
  31. input();
  32. return (width*height)*0.5;
  33. }
  34. };
  35. int main()
  36. {
  37. polygon *p;
  38. char ch;
  39. float a;
  40. int opt;
  41. do
  42. {
  43. cout<<"\nEnter \n1 - area of rectangle\n2 - area of triangle\n";
  44. cin>>opt;
  45. switch(opt)
  46. {
  47. case 1:
  48. p=new rectangle;
  49.  
  50. a=p->area();
  51. cout<<"\nArea of rectangle is : "<<a;
  52. break;
  53. case 2:
  54. p=new triangle;
  55.  
  56. a=p->area();
  57. cout<<"\nArea of triangle is : "<<a;
  58. break;
  59. default:
  60. cout<<"\nInvalid option";
  61. }
  62. cout<<"\nDo you want to continue? [y/n] : ";
  63. cin>>ch;
  64. }while(ch=='y'|ch=='Y');
  65. delete p;
  66. }
Add Comment
Please, Sign In to add comment