Advertisement
PyTimur

Untitled

May 26th, 2022
607
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 Area() = 0;
  14. };
  15.  
  16.  
  17. class Triangle : public Figure{
  18.     double AB, BC, AC;
  19. public:
  20.     Triangle(){}
  21.     Triangle(double AB, double BC, double AC){
  22.         this->AB = AB;
  23.         this->BC = BC;
  24.         this->AC = AC;
  25.         TypeFigure = "Triangle";
  26.     };
  27.     double Area(){
  28.         double p = (AB+BC+AC)/2;
  29.         return sqrt(p*(p-AB)*(p-BC)*(p-AC));
  30.     }
  31. };
  32.  
  33. class Rectangle : public Figure{
  34.     double AB, BC;
  35. public:
  36.     Rectangle(){}
  37.     Rectangle(double AB, double BC){
  38.         this->AB = AB;
  39.         this->BC = BC;
  40.         TypeFigure = "Rectangle";
  41.     }
  42.     double Area(){
  43.         return AB*BC;
  44.     }
  45. };
  46.  
  47. class Circle : public Figure{
  48.     double R;
  49. public:
  50.     Circle(){}
  51.     Circle(double R){
  52.         this->R = R;
  53.         TypeFigure = "Circle";
  54.     }
  55.     double Area(){
  56.         return 3.14*R*R;
  57.     }
  58. };
  59.  
  60. int main(){
  61.     string Type;
  62.     cin >> Type;
  63.     Figure *temp;
  64.     if (Type == "Triangle"){
  65.         double AB, BC, AC;
  66.         cin >> AB >> BC >> AC;
  67.         temp = new Triangle(AB,BC,AC);
  68.         cout << "Area " << temp->GetTypeFigure() <<  " = "<< temp->Area();
  69.     }
  70.     else if (Type == "Rectangle"){
  71.         double AB, BC;
  72.         cin >> AB >> BC;
  73.         temp = new Rectangle(AB, BC);
  74.         cout << "Area " << temp->GetTypeFigure() <<  " = "<< temp->Area();
  75.     }
  76.     else if (Type == "Circle"){
  77.         double R;
  78.         cin >> R;
  79.         temp = new Circle(R);
  80.         cout << "Area " << temp->GetTypeFigure() <<  " = "<< temp->Area();
  81.     }
  82.     else{
  83.         cout << "Input error" << endl;
  84.         return 0;
  85.     }
  86.     delete temp;
  87.     return 0;
  88. }
Advertisement
RAW Paste Data Copied
Advertisement