Vladislav_Bezruk

Untitled

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