Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- const double pi = 3.141592653589793238462643383279502884;
- struct shape {
- double area;
- double circumference;
- shape(double ar = 0., double cir = 0)
- : area{ar}, circumference{cir} { }
- double get_area() const {
- return area;
- }
- double get_circumference() const {
- return circumference;
- }
- bool less_than(const shape& s) const {
- return area < s.area;
- }
- bool smaller_than(const shape& s) const {
- return circumference < s.circumference;
- }
- /**
- * Gonna use this function in operator delegation
- *
- * @param s
- * @return
- */
- virtual bool is_same(const shape& s) const {
- return area == s.area
- && circumference == s.circumference;
- }
- virtual void print() const {
- std::cout << "Shape: area(" << area << "), circ(" << circumference << ")";
- }
- /**
- * Operator < always delegates to less_than call
- *
- * @param s
- * @return
- */
- bool operator <(const shape& s) const {
- return less_than(s);
- }
- };
- struct rectangle
- : shape {
- double a;
- double b;
- rectangle(double x = 0, double y = 0)
- : shape{x * y, 2 * x + 2 * y}, a{x}, b{y} { }
- virtual bool is_same(const shape& s) const override {
- const auto ptr = dynamic_cast<const rectangle*>(&s);
- if (!ptr) return false;
- return shape::is_same(*ptr);
- }
- virtual void print() const override {
- shape::print();
- std::cout << " Rectangle : a(" << a << "), b(" << b << ")";
- }
- };
- struct circle
- : shape {
- double radius;
- circle(double r = 0)
- : shape{r * r * pi, 2 * pi * r}, radius{r} { }
- virtual bool is_same(const shape& s) const override {
- const auto ptr = dynamic_cast<const circle*>(&s);
- if (!ptr) return false;
- return shape::is_same(*ptr);
- }
- virtual void print() const override {
- shape::print();
- std::cout << " Circle : radius(" << radius << ")";
- }
- };
- struct square
- : rectangle {
- double a;
- square(double x = 0)
- : rectangle{x, x}, a{x} { }
- virtual bool is_same(const shape& s) const override {
- const auto ptr = dynamic_cast<const square*>(&s);
- if (!ptr) return false;
- return shape::is_same(*ptr);
- }
- virtual void print() const override {
- rectangle::print();
- std::cout << " Square : a(" << a << ")";
- }
- };
- int main() {
- circle c1{1};
- circle c2{3};
- square s1{6};
- rectangle r1{3, 12};
- shape& sh1r = c1;
- shape& sh2r = s1;
- shape& sh3r = r1;
- std::cout << std::boolalpha << (c1 < c2) << '\n';
- std::cout << s1.less_than(r1) << '\n';
- std::cout << s1.smaller_than(r1) << '\n';
- std::cout << (sh1r < c2) << '\n';
- std::cout << (sh1r < c2) << '\n';
- sh1r.print();
- std::cout << '\n';
- c1.print();
- std::cout << '\n';
- s1.print();
- std::cout << '\n';
- sh3r.print();
- std::cout << '\n';
- std::cout << (s1.is_same(r1)) << '\n';
- std::cout << (s1.is_same(s1)) << '\n';
- std::cout << (s1.is_same(sh1r)) << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement