Advertisement
Zubair4

Untitled

May 27th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Circle;
  4. class Shape
  5. {
  6. public:
  7. virtual double getArea()=0;
  8. virtual void Display()=0;
  9.  
  10. };
  11. class Circle:public Shape
  12. {
  13. double radius;
  14. public:
  15. Circle(double r)
  16. {
  17. radius=r;
  18. }
  19. double getArea()
  20. {
  21. return 3.1416*radius*radius;
  22. }
  23. void Display()
  24. {
  25. cout<<getArea()<<endl;
  26. }
  27. };
  28. class Triangle:public Shape
  29. {
  30. double height;
  31. double ground;
  32. public:
  33. Triangle(double h,double g)
  34. {
  35. height=h;
  36. ground=g;
  37. }
  38. double getArea()
  39. {
  40. return 0.5*height*ground;
  41. }
  42. void Display()
  43. {
  44. cout<<getArea()<<endl;
  45. }
  46. };
  47. int main()
  48. {
  49. int n;
  50. double a,h,g;
  51. // Shape s;
  52. //Circle o(3);
  53. // Triangle
  54. Shape *ptr;
  55. cout<<"Which area of shape you wanna calculate?"<<endl;
  56. cout<<"1. "<<"Circle"<<endl;
  57. cout<<"2. "<<"Triangle"<<endl;
  58. cin>>n;
  59. if(n==1)
  60. {
  61. cout<<"Enter radius of Circle:";
  62. cin>>a;
  63. Circle o(a);
  64. ptr = &o;
  65. ptr->getArea();
  66. cout<<"Area is:";
  67. ptr->Display();
  68. }
  69. else if(n==2)
  70. {
  71. cout<<"Enter height and ground:";
  72. cin>>h;
  73. cin>>g;
  74. Triangle t(h,g);
  75. ptr = &t;
  76. ptr->getArea();
  77. ptr->Display();
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement