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, ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y - h), "#", static_cast<int>(position.y - h), "#",
color); color);
} }
} }
void Circle::nonVirtual() {
std::cout << "Circle nonVirtual" << std::endl;
}

View File

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

View File

@ -20,4 +20,8 @@ void Rectangle::draw() {
ansiConsole.printText(position.x + (width / 2), i, "#", color); ansiConsole.printText(position.x + (width / 2), i, "#", color);
ansiConsole.printText(position.x - (width / 2), i, "#", color); 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: public:
explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE);
void draw() override; void draw() override;
void nonVirtual(void);
}; };

View File

@ -6,4 +6,8 @@ Shape::Shape(Position position, Colors color) {
this->color = color; this->color = color;
} }
Shape::~Shape() = default; Shape::~Shape() = default;
void Shape::nonVirtual() {
std::cout << "Shape nonVirtual" << std::endl;
}

View File

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

View File

@ -5,6 +5,16 @@
#include "Rectangle.h" #include "Rectangle.h"
#include "Scene.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) { int main(int argc, char **argv) {
std::vector<Shape*> shapes; 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(15, 7, Colors::WHITE));
shapes.push_back(new Point(7, 5, Colors::WHITE)); shapes.push_back(new Point(7, 5, Colors::WHITE));
auto *s = new Scene(shapes); auto *s = new Scene(shapes);
s->draw(); s->draw();
delete s; 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; return 0;
} }