Skip to content

Instantly share code, notes, and snippets.

@benjaminparnell
Created February 2, 2016 17:39
Show Gist options
  • Select an option

  • Save benjaminparnell/880a15f40cc8b1029b5a to your computer and use it in GitHub Desktop.

Select an option

Save benjaminparnell/880a15f40cc8b1029b5a to your computer and use it in GitHub Desktop.
#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