Merge remote-tracking branch 'origin/master'

This commit is contained in:
Johannes Theiner 2018-11-25 11:42:39 +01:00
commit 644521b3a9
7 changed files with 31 additions and 3 deletions

View File

@ -21,6 +21,9 @@ void Circle::draw() {
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y - h), "#",
color);
}
}
}
void Circle::nonVirtual() {
std::cout << "Circle nonVirtual" << std::endl;
}

View File

@ -9,6 +9,7 @@ protected:
public:
explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN);
void draw() override;
void nonVirtual();
};
#endif //C_C_CIRCLE_H

View File

@ -21,3 +21,7 @@ void Rectangle::draw() {
ansiConsole.printText(position.x - (width / 2), i, "#", color);
}
}
void Rectangle::nonVirtual() {
std::cout << "Rectangle nonVirtual" << std::endl;
}

View File

@ -12,6 +12,7 @@ protected:
public:
explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE);
void draw() override;
void nonVirtual(void);
};

View File

@ -7,3 +7,7 @@ Shape::Shape(Position position, Colors color) {
}
Shape::~Shape() = default;
void Shape::nonVirtual() {
std::cout << "Shape nonVirtual" << std::endl;
}

View File

@ -12,6 +12,7 @@ public:
Shape(Position position, Colors color);
virtual ~Shape();
virtual void draw() = 0;
void nonVirtual(void);
};
#endif

View File

@ -5,6 +5,16 @@
#include "Rectangle.h"
#include "Scene.h"
void invokeVirtually(Shape* shape) {
if(auto *r = dynamic_cast<Rectangle*>(shape)) {
r->nonVirtual();
}else if(auto *c = dynamic_cast<Circle*>(shape)) {
c->nonVirtual();
}else {
shape->nonVirtual();
}
}
int main(int argc, char **argv) {
std::vector<Shape*> shapes;
@ -23,12 +33,16 @@ int main(int argc, char **argv) {
shapes.push_back(new Point(15, 7, Colors::WHITE));
shapes.push_back(new Point(7, 5, Colors::WHITE));
auto *s = new Scene(shapes);
s->draw();
delete s;
invokeVirtually(new Circle(30, 10, 10, Colors::RED));
invokeVirtually(new Rectangle(5, 5, 5, 5, Colors::BLUE));
invokeVirtually(new Point(5, 5, Colors::YELLOW));
return 0;
}