Advertisement
PyTimur

Area

May 15th, 2022
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.61 KB | None | 0 0
  1. #include "iostream"
  2. #include "cmath"
  3.  
  4. using namespace std;
  5.  
  6. class Figure{
  7. protected:
  8.     string TypeFigure;
  9. public:
  10.     Figure(){}
  11.     virtual ~Figure() {}
  12.     string GetTypeFigure() { return TypeFigure; }
  13.     virtual double GetArea() = 0;
  14.  
  15. };
  16.  
  17.  
  18. class Triangle : public Figure{
  19. protected:
  20.     double AB, BC, AC;
  21. public:
  22.     Triangle(){}
  23.     Triangle(double AB, double BC, double AC){
  24.         this->AB = AB;
  25.         this->BC = BC;
  26.         this->AC = AC;
  27.         TypeFigure = "Triangle";
  28.     };
  29.     double GetArea(){
  30.         double p = (AB+BC+AC)/2;
  31.         return sqrt(p*(p-AB)*(p-BC)*(p-AC));
  32.     }
  33. };
  34.  
  35. class Rectangle : public Figure{
  36. protected:
  37.     double AB, BC;
  38. public:
  39.     Rectangle(){}
  40.     Rectangle(double AB, double BC){
  41.         this->AB = AB;
  42.         this->BC = BC;
  43.         TypeFigure = "Rectangle";
  44.     }
  45.     double GetArea(){
  46.         return AB*BC;
  47.     }
  48. };
  49.  
  50. class Circle : public Figure{
  51. protected:
  52.     double R;
  53. public:
  54.     Circle(){}
  55.     Circle(double R): R(R) {TypeFigure = "Circle";}
  56.     double GetArea(){
  57.         return M_PI*R*R;
  58.     }
  59. };
  60.  
  61. int main(){
  62.     string t;
  63.     cout << "What type of figure?" << '\n';
  64.     cin >> t;
  65.     Figure *p;
  66.     if (t == "Triangle"){
  67.         double AB, BC, AC;
  68.         cin >> AB >> BC >> AC;
  69.         p = new Triangle(AB,BC,AC);
  70.     }
  71.     else if (t == "Rectangle"){
  72.         double AB, BC;
  73.         cin >> AB >> BC;
  74.         p = new Rectangle(AB, BC);
  75.     }
  76.     else {
  77.         int r;
  78.         cin >> r;
  79.         p = new Circle(r);
  80.     }
  81.     cout << "Area " << p->GetTypeFigure() <<  " = "<< p->GetArea();
  82.     return 0;
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement