Untitled
unknown
plain_text
a year ago
2.4 kB
2
Indexable
drawing.cpp #include "drawing.h" #include "point.h" #include "polygon.h" #include <iostream> #include "shape.h" using namespace std; Drawing::Drawing() { } Drawing::~Drawing() { for (auto val : this->shapes) delete val; } void Drawing::AddPoint(Point *point) { this->shapes.push_back(new Point(*point)); } void Drawing::AddPolygon(Polygon *polygon) { this->shapes.push_back(new Polygon(*polygon)); } void Drawing::ShowDrawing(void) { for (auto val: this->shapes) { val->ShowShape(); cout << endl; } } void Drawing::MoveByX(int x) { for (auto shape : this->shapes) { IMove *ptr = dynamic_cast<IMove *>(shape); ptr->MoveX(x); } } void Drawing::MoveByXY(int x, int y) { for (auto shape : this->shapes) { IMove *ptr = dynamic_cast<IMove *>(shape); ptr->Move(x, y); } } void Drawing::MoveByY(int y) { for (auto shape : this->shapes) { IMove *ptr = dynamic_cast<IMove *>(shape); ptr->MoveY(y); } } Shape &Drawing::operator[](int index) { return *this->shapes[index]; } ///////////////////////////////////////////////////////////// main.cpp #include "point.h" #include "polygon.h" #include "drawing.h" #include "shape.h" #include <iostream> using namespace std; int main() { Point pt1(100, 100); Point pt2(200, 200); Point pt3(300, 300); Polygon poly1(&pt1, &pt2, &pt3); Drawing drawing; drawing.AddPoint(&pt1); drawing.AddPoint(&pt2); drawing.AddPoint(&pt3); drawing.AddPolygon(&poly1); // Now perform operation on the object // Get a shape from the drawing Shape &myShape = drawing[2]; // Move that Shape IMove *ptr = dynamic_cast<IMove *> (&myShape); ptr->Move(10, 30); IRotate *sPtr = dynamic_cast<IRotate *>(ptr); if (sPtr == nullptr) { cout << "IROTATE Interface is not implemented" << endl; } else { cout << "Rotating SHAPE: "; sPtr->Rotate(100); cout << endl; } drawing.ShowDrawing(); } //////////////////////// shape.h #pragma once #include <iostream> struct IMove { virtual void Move(int x, int y) = 0; virtual void MoveX(int x) = 0; virtual void MoveY(int y) = 0; }; struct IRotate { virtual void Rotate(int angle) = 0; virtual void Rotate3D(int x, int y, int z) = 0; }; struct Shape { virtual void ShowShape(void) = 0; };
Editor is loading...
Leave a Comment