Created
February 2, 2016 17:39
-
-
Save benjaminparnell/880a15f40cc8b1029b5a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| class Shape { | |
| public: | |
| virtual void print() = 0; | |
| }; | |
| class Square : public Shape { | |
| virtual void print() { | |
| std::cout << "Square" << std::endl; | |
| } | |
| }; | |
| class Rectangle : public Shape { | |
| virtual void print() { | |
| std::cout << "Rectangle" << std::endl; | |
| } | |
| }; | |
| class CompositeShape : public Shape { | |
| public: | |
| virtual void print() { | |
| for (auto &shape : this->shapes) { | |
| shape->print(); | |
| } | |
| } | |
| void add(Shape *shape) { | |
| this->shapes.push_back(shape); | |
| } | |
| private: | |
| std::vector<Shape*> shapes; | |
| }; | |
| int main() { | |
| CompositeShape* compShape = new CompositeShape; | |
| Rectangle* rect = new Rectangle; | |
| Square* sq = new Square; | |
| Rectangle* rect2 = new Rectangle; | |
| compShape->add(rect); | |
| compShape->add(sq); | |
| compShape->add(rect2); | |
| compShape->print(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment