Advertisement
Vladislav_Bezruk

Untitled

Nov 11th, 2021
712
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.93 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class Figure {
  6.     protected:
  7.         float s;
  8.    
  9.     public:
  10.         void show() {
  11.             cout << "Info about figure:" << endl;
  12.             calculate();
  13.             info();
  14.         }
  15.        
  16.         virtual void calculate() = 0;
  17.        
  18.         virtual void info() = 0;
  19. };
  20.  
  21. class Rectangle : public Figure {
  22.     private:
  23.         float a;
  24.        
  25.         float b;
  26.        
  27.     public:
  28.         Rectangle() {}
  29.        
  30.         Rectangle(float _a, float _b) : a(_a), b(_b) {}
  31.        
  32.         void calculate() {
  33.             s = a * b;
  34.            
  35.             cout << "Area of rectangle = " << s << endl;
  36.             cout << endl;
  37.         }
  38.        
  39.         void info() {
  40.             cout << "Sides of rectangle:" << endl;
  41.             cout << "\ta = " << a << endl;
  42.             cout << "\tb = " << b << endl;
  43.             cout << endl;
  44.         }
  45.        
  46.         friend istream& operator >> (istream& is, Rectangle& x);
  47.        
  48.         bool operator > (const Rectangle& x) { return a > x.a && b > x.b; }
  49. };
  50.  
  51. class Trapeze : public Figure {
  52.     private:
  53.         float a;
  54.        
  55.         float b;
  56.        
  57.         float h;
  58.    
  59.     public:
  60.         Trapeze() {}
  61.        
  62.         Trapeze(float _a, float _b, float _h) : a(_a), b(_b), h(_h) {}
  63.        
  64.         void calculate() {
  65.             s = h * (a + b) / 2;
  66.            
  67.             cout << "Area of trapeze = " << s << endl;
  68.             cout << endl;
  69.         }
  70.        
  71.         void info() {
  72.             cout << "Sides of trapeze:" << endl;
  73.             cout << "\ta = " << a << endl;
  74.             cout << "\tb = " << b << endl;
  75.             cout << "\th = " << h << endl;
  76.             cout << endl;
  77.         }
  78.        
  79.         friend istream& operator >> (istream& is, Trapeze& x);
  80.        
  81.         bool operator > (const Trapeze& x) { return a > x.a && b > x.b && h > x.h; }
  82. };
  83.  
  84. istream& operator >> (istream& is, Rectangle& x) {
  85.     cout << "Enter sides of rectangle:" << endl;
  86.     cout << "\ta = "; is >> x.a;
  87.     cout << "\tb = "; is >> x.b;
  88.     cout << endl;
  89.    
  90.     return is;
  91. }
  92.  
  93. istream& operator >> (istream& is, Trapeze& x) {
  94.     cout << "Enter sides of trapeze:" << endl;
  95.     cout << "\ta = "; is >> x.a;
  96.     cout << "\tb = "; is >> x.b;
  97.     cout << "\th = "; is >> x.h;
  98.     cout << endl;
  99.    
  100.     return is;
  101. }
  102.  
  103. int main() {
  104.     cout << "===Work with arrays===" << endl << endl;
  105.    
  106.     int n;
  107.    
  108.     cout << "Enter count of objects:" << endl;
  109.     cin >> n;
  110.     cout << endl;
  111.    
  112.     Rectangle rectArr[n];
  113.    
  114.     for (int i = 0; i < n; i++) {
  115.         cout << "Enter info about " << i + 1 << " rectangle:" << endl;
  116.         cin >> rectArr[i];
  117.     }
  118.    
  119.     for (int i = 1; i < n; i++) {
  120.         cout << "Comparing (operator >) " << i << " & " << i + 1 << " rectangles: "<< endl;
  121.         cout << "result: " << (rectArr[i - 1] > rectArr[i]) << endl;
  122.         cout << endl;
  123.     }
  124.    
  125.     Trapeze trapzArr[n];
  126.    
  127.     for (int i = 0; i < n; i++) {
  128.         cout << "Enter info about " << i + 1 << " trapeze:" << endl;
  129.         cin >> trapzArr[i];
  130.     }
  131.    
  132.     for (int i = 1; i < n; i++) {
  133.         cout << "Comparing (operator >) " << i << " & " << i + 1 << " trapezes: "<< endl;
  134.         cout << "result: " << (trapzArr[i - 1] > trapzArr[i]) << endl;
  135.         cout << endl;
  136.     }
  137.    
  138.     cout << "===Work using the base class pointer===" << endl << endl;
  139.    
  140.     Figure* figr = new Rectangle(3, 4);
  141.    
  142.     figr->show();
  143.    
  144.     figr = new Trapeze(3, 4, 2);
  145.    
  146.     figr->show();
  147.    
  148.     return 0;
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement