Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3.  
  4. const double pi = 3.141592653589793238462643383279502884;
  5.  
  6. struct shape {
  7.     double area;
  8.     double circumference;
  9.  
  10.     shape(double ar = 0., double cir = 0)
  11.             : area{ar}, circumference{cir} { }
  12.  
  13.     double get_area() const {
  14.         return area;
  15.     }
  16.  
  17.     double get_circumference() const {
  18.         return circumference;
  19.     }
  20.  
  21.     bool less_than(const shape& s) const {
  22.         return area < s.area;
  23.     }
  24.  
  25.     bool smaller_than(const shape& s) const {
  26.         return circumference < s.circumference;
  27.     }
  28.  
  29.     /**
  30.      * Gonna use this function in operator delegation
  31.      *
  32.      * @param s
  33.      * @return
  34.      */
  35.     virtual bool is_same(const shape& s) const {
  36.         return area == s.area
  37.                && circumference == s.circumference;
  38.     }
  39.  
  40.     virtual void print() const {
  41.         std::cout << "Shape: area(" << area << "), circ(" << circumference << ")";
  42.     }
  43.  
  44.     /**
  45.      * Operator < always delegates to less_than call
  46.      *
  47.      * @param s
  48.      * @return
  49.      */
  50.     bool operator <(const shape& s) const {
  51.         return less_than(s);
  52.     }
  53. };
  54.  
  55. struct rectangle
  56.         : shape {
  57.     double a;
  58.     double b;
  59.  
  60.     rectangle(double x = 0, double y = 0)
  61.             : shape{x * y, 2 * x + 2 * y}, a{x}, b{y} { }
  62.  
  63.     virtual bool is_same(const shape& s) const override {
  64.         const auto ptr = dynamic_cast<const rectangle*>(&s);
  65.         if (!ptr) return false;
  66.         return shape::is_same(*ptr);
  67.     }
  68.  
  69.     virtual void print() const override {
  70.         shape::print();
  71.         std::cout << " Rectangle : a(" << a << "), b(" << b << ")";
  72.     }
  73. };
  74.  
  75. struct circle
  76.         : shape {
  77.     double radius;
  78.  
  79.     circle(double r = 0)
  80.             : shape{r * r * pi, 2 * pi * r}, radius{r} { }
  81.  
  82.     virtual bool is_same(const shape& s) const override {
  83.         const auto ptr = dynamic_cast<const circle*>(&s);
  84.         if (!ptr) return false;
  85.         return shape::is_same(*ptr);
  86.     }
  87.  
  88.     virtual void print() const override {
  89.         shape::print();
  90.         std::cout << " Circle : radius(" << radius << ")";
  91.     }
  92.  
  93. };
  94.  
  95. struct square
  96.         : rectangle {
  97.     double a;
  98.  
  99.     square(double x = 0)
  100.             : rectangle{x, x}, a{x} { }
  101.  
  102.     virtual bool is_same(const shape& s) const override {
  103.         const auto ptr = dynamic_cast<const square*>(&s);
  104.         if (!ptr) return false;
  105.         return shape::is_same(*ptr);
  106.     }
  107.  
  108.     virtual void print() const override {
  109.         rectangle::print();
  110.         std::cout << " Square : a(" << a << ")";
  111.     }
  112. };
  113.  
  114. int main() {
  115.     circle c1{1};
  116.     circle c2{3};
  117.  
  118.     square s1{6};
  119.     rectangle r1{3, 12};
  120.  
  121.     shape& sh1r = c1;
  122.     shape& sh2r = s1;
  123.     shape& sh3r = r1;
  124.  
  125.     std::cout << std::boolalpha << (c1 < c2) << '\n';
  126.     std::cout << s1.less_than(r1) << '\n';
  127.     std::cout << s1.smaller_than(r1) << '\n';
  128.  
  129.     std::cout << (sh1r < c2) << '\n';
  130.     std::cout << (sh1r < c2) << '\n';
  131.  
  132.  
  133.     sh1r.print();
  134.     std::cout << '\n';
  135.     c1.print();
  136.     std::cout << '\n';
  137.     s1.print();
  138.     std::cout << '\n';
  139.     sh3r.print();
  140.     std::cout << '\n';
  141.  
  142.     std::cout << (s1.is_same(r1)) << '\n';
  143.     std::cout << (s1.is_same(s1)) << '\n';
  144.     std::cout << (s1.is_same(sh1r)) << '\n';
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement