GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — learnmoderncpp-tutorial/modules/09-shape.cpp at main · cpp-tutor/learnmoderncpp-tutorial Expand file tree/ Copy path09-shape.cppMore file actions 86 lines (77 loc) · 2.61 KB / Copy path09-shape.cppFile metadata and controls86 lines (77 loc) · 2.61 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 // 09-shape.cpp : Shape class hierarchy demonstrating polymorphism import std; using namespace std; class Shape { public: struct Point; Shape(int sides) : sides{ sides } {} Shape(int sides, Point center) : sides{ sides }, center{ center } {} virtual void draw(ostream& os) const = 0; virtual string getSides() const { return to_string(sides); } void moveBy(int dx, int dy) { center.x += dx; center.y += dy; } const Point& getCenter() const { return center; } virtual ~Shape() { cerr << "~Shape()\n"; } struct Point { int x{}, y{}; }; private: int sides; Point center; }; ostream& operator<< (ostream& os, const Shape::Point& pt) { return os << '(' << pt.x << ',' << pt.y << ')'; } class Triangle final : public Shape { public: Triangle(int side) : Shape{ 3 }, side{ side } {} Triangle(int x, int y, int side) : Shape{ 3, {x, y} }, side{ side } {} virtual void draw(ostream& os) const override { os << " /\\\n/__\\\nSide: " << side << "\nAt: " << getCenter() << '\n'; } private: int side; }; class Circle : public Shape { public: Circle(int radius) : Shape{ 0 }, radius{ radius } {} Circle(int x, int y, int radius) : Shape{ 0, {x, y} }, radius{ radius } {} virtual void draw(ostream& os) const override final { os << " _\n(_)\nRadius: " << radius << "\nAt: " << getCenter() << '\n'; } virtual string getSides() const override final { return "infinite"; } private: int radius; }; class Rectangle : public Shape { public: Rectangle(int side_x, int side_y) : Shape{ 4 }, side_x{ side_x }, side_y{ side_y } {} Rectangle(int x, int y, int side_x, int side_y) : Shape{ 4, {x ,y} }, side_x{ side_x }, side_y{ side_y } {} virtual void draw(ostream& os) const override { os << " ____\n|____|\nSize: " << side_x << 'x' << side_y << "\nAt: " << getCenter() << '\n'; } protected: int side_x, side_y; }; class Square final : public Rectangle { public: Square(int side) : Rectangle{ side, side } {} Square(int x, int y, int side) : Rectangle{ x, y, side, side } {} virtual void draw(ostream& os) const override final { os << " _\n|_|\nSide: " << side_x << "\nAt: " << getCenter() << '\n'; } }; int main() { vector<Shape*> shapes; shapes.push_back(new Circle{ 10 }); shapes.push_back(new Triangle{ 10, 20, 15 }); shapes.push_back(new Rectangle{ 10, 5 }); shapes.push_back(new Square{ 25, 100, 50 }); shapes[0]->moveBy(20, 50); for (auto& s : shapes) { s->draw(cout); cout << "Sides: " << s->getSides() << '\n'; delete s; s = nullptr; } } |
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |