Advertisement
Proff_Ust

class_triangle

Dec 15th, 2019
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <sstream>
  4. using namespace std;
  5. class CTriangle
  6. {
  7. protected:
  8.     float a;
  9.     float b;
  10.     float c;
  11.     string stringInfo()
  12.     {
  13.         ostringstream temp;
  14.         temp<<"Первая сторона: "<<this->a<<", ";
  15.         temp<<"Вторая сторона: "<<this->b<<", ";
  16.         temp<<"Третья сторона: "<<this->c<<endl;
  17.         return temp.str();
  18.     }
  19. public:
  20.     CTriangle()
  21.     {
  22.         this->a = 0;
  23.         this->b = 0;
  24.         this->b = 0;
  25.     }
  26.  
  27.     CTriangle(float newA, float newB, float newC)
  28.     {
  29.         this->a = newA;
  30.         this->b = newB;
  31.         this->c = newC;
  32.     }
  33.  
  34.     float S()
  35.     {
  36.         float p = P()/2;
  37.         return sqrt(p*(p-a)*(p-b)*(p-c));
  38.     }
  39.  
  40.     float P()
  41.     {
  42.         return this->a + this->b + this->c;
  43.     }
  44.  
  45.     string toString()
  46.     {
  47.         ostringstream temp;
  48.         temp<<"Данные о треугольнике:"<<endl;
  49.         temp<<this->stringInfo()<<endl;
  50.         return temp.str();
  51.     }
  52.  
  53.     ~CTriangle(){};
  54. };
  55.  
  56. class Crectangle:CTriangle
  57. {
  58. private:
  59.     float d;
  60.     float e;
  61.     float f;
  62. public:
  63.     Crectangle()
  64.     {
  65.         this->d = 0;
  66.         this->e = 0;
  67.         this->f = 0;
  68.     }
  69.     Crectangle(float newA, float newB, float newC,
  70.                float newD, float newE, float newF):CTriangle(
  71.                                                 newA, newB, newC)
  72.     {
  73.         this->d = newD;
  74.         this->e = newE;
  75.         this->f = newF;
  76.     }
  77.  
  78.     float S()
  79.     {
  80.         return sqrt((4*pow(this->e,2)*pow(this->f,2)-
  81.                      (pow(this->b,2)+pow(this->d,2)-pow(this->a,2)-pow(this->c,2    )))/16);
  82.     }
  83.  
  84.     float P()
  85.     {
  86.         return CTriangle::P() + this->d;
  87.     }
  88.  
  89.     string toString()
  90.     {
  91.  
  92.         ostringstream temp;
  93.         temp<<"Данные о четырехугольнике:"<<endl;
  94.         temp<<CTriangle::stringInfo();
  95.         temp<<"Четвертая сторона: "<<this->d<<", "<<endl;
  96.         temp<<"Первая диагональ: "<<this->e<<", ";
  97.         temp<<"Вторая диагональ: "<<this->f<<endl;
  98.         return temp.str();
  99.     }
  100.  
  101.     ~Crectangle(){};
  102. };
  103.  
  104. int main()
  105. {
  106.     setlocale(0,"Russian");
  107.     CTriangle * triangle = new CTriangle(3,4,5);
  108.     Crectangle* rectangle = new Crectangle(1,1,1,1,1.414213,1.414213);
  109.     cout<<triangle->toString();
  110.     cout<<rectangle->toString();
  111.     cout<<"Площадь треугольника: "<<triangle->S()<<", периметр треугольника: "<<triangle->P()<<endl;
  112.     cout<<"Площадь четыререхугольника: "<<rectangle->S()<<", периметр четырехугольника: "<<rectangle->P()<<endl;
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement