From 33defc82a5e1101484c3300ea3ad2f5d837feec6 Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 20 Nov 2018 10:37:40 +0100 Subject: [PATCH] 06_POLY: Testat bestanden --- src/06_POLY/Testat/Circle.cpp | 5 ++++- src/06_POLY/Testat/Circle.h | 1 + src/06_POLY/Testat/Rectangle.cpp | 4 ++++ src/06_POLY/Testat/Rectangle.h | 1 + src/06_POLY/Testat/Shape.cpp | 6 +++++- src/06_POLY/Testat/Shape.h | 1 + src/06_POLY/Testat/shapes_main.cpp | 16 +++++++++++++++- 7 files changed, 31 insertions(+), 3 deletions(-) diff --git a/src/06_POLY/Testat/Circle.cpp b/src/06_POLY/Testat/Circle.cpp index 5ef58f7..d3c8a18 100644 --- a/src/06_POLY/Testat/Circle.cpp +++ b/src/06_POLY/Testat/Circle.cpp @@ -21,6 +21,9 @@ void Circle::draw() { ansiConsole.printText(position.x + int(x_relative) - _radius / 2, static_cast(position.y - h), "#", color); - } } + +void Circle::nonVirtual() { + std::cout << "Circle nonVirtual" << std::endl; +} diff --git a/src/06_POLY/Testat/Circle.h b/src/06_POLY/Testat/Circle.h index f0186d2..abf32b4 100644 --- a/src/06_POLY/Testat/Circle.h +++ b/src/06_POLY/Testat/Circle.h @@ -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 diff --git a/src/06_POLY/Testat/Rectangle.cpp b/src/06_POLY/Testat/Rectangle.cpp index 7aaa71a..37c8b81 100644 --- a/src/06_POLY/Testat/Rectangle.cpp +++ b/src/06_POLY/Testat/Rectangle.cpp @@ -20,4 +20,8 @@ void Rectangle::draw() { 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; } \ No newline at end of file diff --git a/src/06_POLY/Testat/Rectangle.h b/src/06_POLY/Testat/Rectangle.h index f8775b9..eb8f86d 100644 --- a/src/06_POLY/Testat/Rectangle.h +++ b/src/06_POLY/Testat/Rectangle.h @@ -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); }; diff --git a/src/06_POLY/Testat/Shape.cpp b/src/06_POLY/Testat/Shape.cpp index 6fa576c..601a599 100644 --- a/src/06_POLY/Testat/Shape.cpp +++ b/src/06_POLY/Testat/Shape.cpp @@ -6,4 +6,8 @@ Shape::Shape(Position position, Colors color) { this->color = color; } -Shape::~Shape() = default; \ No newline at end of file +Shape::~Shape() = default; + +void Shape::nonVirtual() { + std::cout << "Shape nonVirtual" << std::endl; +} \ No newline at end of file diff --git a/src/06_POLY/Testat/Shape.h b/src/06_POLY/Testat/Shape.h index 2c803d3..98382b2 100644 --- a/src/06_POLY/Testat/Shape.h +++ b/src/06_POLY/Testat/Shape.h @@ -12,6 +12,7 @@ public: Shape(Position position, Colors color); virtual ~Shape(); virtual void draw() = 0; + void nonVirtual(void); }; #endif \ No newline at end of file diff --git a/src/06_POLY/Testat/shapes_main.cpp b/src/06_POLY/Testat/shapes_main.cpp index 2c96230..51feb1b 100644 --- a/src/06_POLY/Testat/shapes_main.cpp +++ b/src/06_POLY/Testat/shapes_main.cpp @@ -5,6 +5,16 @@ #include "Rectangle.h" #include "Scene.h" +void invokeVirtually(Shape* shape) { + if(auto *r = dynamic_cast(shape)) { + r->nonVirtual(); + }else if(auto *c = dynamic_cast(shape)) { + c->nonVirtual(); + }else { + shape->nonVirtual(); + } +} + int main(int argc, char **argv) { std::vector 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; } \ No newline at end of file