Vladislav_Bezruk

Untitled

Nov 11th, 2021 (edited)
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.94 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. using namespace std;
  5.  
  6. class Figure {
  7.     protected:
  8.         float a;
  9.         float b;
  10.         float p;
  11.    
  12.     public:
  13.         void show() {
  14.             cout << "Info:" << endl;
  15.             calculate();
  16.             info();
  17.         }
  18.        
  19.         virtual float calculate() = 0;
  20.        
  21.         virtual void info() = 0;
  22. };
  23.  
  24. class IsoscelesTriangle : public Figure {
  25.     public:
  26.         IsoscelesTriangle() {a = b = 0;}
  27.        
  28.         IsoscelesTriangle(float x, float y) {
  29.             a = x;
  30.             b = y;
  31.         }
  32.        
  33.         float calculate() {
  34.             p = a + 2 * b;
  35.            
  36.             cout << "p of isosceles triangle = " << p << endl;
  37.             cout << endl;
  38.            
  39.             return p;
  40.         }
  41.        
  42.         void info() {
  43.             cout << "Info about isosceles triangle:" << endl;
  44.             cout << "a = " << a << endl;
  45.             cout << "b = " << b << endl;
  46.             cout << endl;
  47.         }
  48.        
  49.         friend istream& operator >> (istream& is, IsoscelesTriangle& a);
  50.        
  51.         bool operator > (const IsoscelesTriangle& a) {return this->a > a.a && b > a.b;}
  52. };
  53.  
  54. istream& operator >> (istream& is, IsoscelesTriangle& a) {
  55.     cout << "Enter info about isosceles triangle:" << endl;
  56.     cout << "a = "; is >> a.a;
  57.     cout << "b = "; is >> a.b;
  58.     cout << endl;
  59.    
  60.     return is;
  61. }
  62.  
  63. class RightTriangle : public Figure {
  64.     public:
  65.         RightTriangle() {a = b = 0;}
  66.        
  67.         RightTriangle(float x, float y) {
  68.             a = x;
  69.             b = y;
  70.         }
  71.        
  72.         float calculate() {
  73.             p = a + b + sqrt(a * a + b * b);
  74.            
  75.             cout << "p of right triangle = " << p << endl;
  76.             cout << endl;
  77.            
  78.             return p;
  79.         }
  80.        
  81.         void info() {
  82.             cout << "Info about right triangle:" << endl;
  83.             cout << "a = " << a << endl;
  84.             cout << "b = " << b << endl;
  85.             cout << endl;
  86.         }
  87.        
  88.         friend istream& operator >> (istream& is, RightTriangle& a);
  89.        
  90.         bool operator > (const RightTriangle& a) {return this->a > a.a && b > a.b;}
  91. };
  92.  
  93. istream& operator >> (istream& is, RightTriangle& a) {
  94.     cout << "Enter info about right triangle:" << endl;
  95.     cout << "a = "; is >> a.a;
  96.     cout << "b = "; is >> a.b;
  97.     cout << endl;
  98.    
  99.     return is;
  100. }
  101.  
  102. int main() {
  103.    
  104.     int count;
  105.     bool res;
  106.    
  107.     cout << "Enter count of objects: ";
  108.     cin >> count;
  109.    
  110.     IsoscelesTriangle arr1[count];
  111.    
  112.     for (int i = 0; i < count; i++) {
  113.         cout << "Enter " << i + 1 << " isosceles triangle:" << endl;
  114.         cin >> arr1[i];
  115.     }
  116.    
  117.     for (int i = 1; i < count; i++) {
  118.         res = arr1[i - 1] > arr1[i];
  119.        
  120.         cout << "(" << i << " obj > " << i + 1 << " obj) = ";
  121.        
  122.         if (res) cout << "true" << endl;
  123.         else cout << "false" << endl;
  124.     }
  125.    
  126.     cout << endl;
  127.    
  128.     RightTriangle arr2[count];
  129.    
  130.     for (int i = 0; i < count; i++) {
  131.         cout << "Enter " << i + 1 << " right triangle:" << endl;
  132.         cin >> arr2[i];
  133.     }
  134.    
  135.     for (int i = 1; i < count; i++) {
  136.         res = arr2[i - 1] > arr2[i];
  137.        
  138.         cout << "(" << i << " obj > " << i + 1 << " obj) = ";
  139.        
  140.         if (res) cout << "true" << endl;
  141.         else cout << "false" << endl;
  142.     }
  143.    
  144.     cout << "------" << endl << endl;
  145.    
  146.     Figure* obj;
  147.    
  148.     obj = new IsoscelesTriangle(2, 3);
  149.    
  150.     obj->show();
  151.    
  152.     obj = new RightTriangle(3, 4);
  153.    
  154.     obj->show();
  155.    
  156. }
Add Comment
Please, Sign In to add comment