From 8d01cff127684d43f4530fec15fc6d7f2cee199d Mon Sep 17 00:00:00 2001 From: joethei Date: Fri, 9 Nov 2018 21:43:48 +0100 Subject: [PATCH] 05_OOb: Anfang Bearbeitung MP --- src/05_OO/b/CMakeLists.txt | 28 +--- src/05_OO/b/MP/Circle.cpp | 26 ---- src/05_OO/b/MP/Circle.h | 14 -- src/05_OO/b/MP/Point.cpp | 8 - src/05_OO/b/MP/Point.h | 14 -- src/05_OO/b/MP/Position.h | 14 -- src/05_OO/b/MP/Rectangle.cpp | 23 --- src/05_OO/b/MP/Rectangle.h | 18 --- src/05_OO/b/MP/Scene.cpp | 19 --- src/05_OO/b/MP/Scene.h | 17 --- src/05_OO/b/MP/Shape.cpp | 9 -- src/05_OO/b/MP/Shape.h | 17 --- src/05_OO/b/MP/main_mp4_OO_b.cpp | 220 +++++++++++++++++++++++++++ src/05_OO/b/MP/shapes_main.cpp | 34 ----- src/05_OO/b/Testat/Circle.cpp | 26 ---- src/05_OO/b/Testat/Circle.h | 14 -- src/05_OO/b/Testat/Point.cpp | 8 - src/05_OO/b/Testat/Point.h | 14 -- src/05_OO/b/Testat/Position.h | 14 -- src/05_OO/b/Testat/Rectangle.cpp | 23 --- src/05_OO/b/Testat/Rectangle.h | 18 --- src/05_OO/b/Testat/Scene.cpp | 19 --- src/05_OO/b/Testat/Scene.h | 17 --- src/05_OO/b/Testat/Shape.cpp | 9 -- src/05_OO/b/Testat/Shape.h | 17 --- src/05_OO/b/Testat/main_mp4_OO_b.cpp | 198 ++++++++++++++++++++++++ src/05_OO/b/Testat/shapes_main.cpp | 34 ----- 27 files changed, 420 insertions(+), 452 deletions(-) delete mode 100644 src/05_OO/b/MP/Circle.cpp delete mode 100644 src/05_OO/b/MP/Circle.h delete mode 100644 src/05_OO/b/MP/Point.cpp delete mode 100644 src/05_OO/b/MP/Point.h delete mode 100644 src/05_OO/b/MP/Position.h delete mode 100644 src/05_OO/b/MP/Rectangle.cpp delete mode 100644 src/05_OO/b/MP/Rectangle.h delete mode 100644 src/05_OO/b/MP/Scene.cpp delete mode 100644 src/05_OO/b/MP/Scene.h delete mode 100644 src/05_OO/b/MP/Shape.cpp delete mode 100644 src/05_OO/b/MP/Shape.h create mode 100644 src/05_OO/b/MP/main_mp4_OO_b.cpp delete mode 100644 src/05_OO/b/MP/shapes_main.cpp delete mode 100644 src/05_OO/b/Testat/Circle.cpp delete mode 100644 src/05_OO/b/Testat/Circle.h delete mode 100644 src/05_OO/b/Testat/Point.cpp delete mode 100644 src/05_OO/b/Testat/Point.h delete mode 100644 src/05_OO/b/Testat/Position.h delete mode 100644 src/05_OO/b/Testat/Rectangle.cpp delete mode 100644 src/05_OO/b/Testat/Rectangle.h delete mode 100644 src/05_OO/b/Testat/Scene.cpp delete mode 100644 src/05_OO/b/Testat/Scene.h delete mode 100644 src/05_OO/b/Testat/Shape.cpp delete mode 100644 src/05_OO/b/Testat/Shape.h create mode 100644 src/05_OO/b/Testat/main_mp4_OO_b.cpp delete mode 100644 src/05_OO/b/Testat/shapes_main.cpp diff --git a/src/05_OO/b/CMakeLists.txt b/src/05_OO/b/CMakeLists.txt index f5a417e..336b04e 100644 --- a/src/05_OO/b/CMakeLists.txt +++ b/src/05_OO/b/CMakeLists.txt @@ -1,31 +1,7 @@ add_executable(05_OOb_MP - MP/shapes_main.cpp - MP/Shape.cpp - MP/Position.h - MP/Shape.h - MP/Point.cpp - MP/Point.h - MP/Circle.cpp - MP/Circle.h - MP/Rectangle.cpp - MP/Rectangle.h - MP/Scene.cpp - MP/Scene.h - ../../helpers/AnsiConsole.cpp + MP/main_mp4_OO_b.cpp ) add_executable(05_OOb_Testat - Testat/shapes_main.cpp - Testat/Shape.cpp - Testat/Position.h - Testat/Shape.h - Testat/Point.cpp - Testat/Point.h - Testat/Circle.cpp - Testat/Circle.h - Testat/Rectangle.cpp - Testat/Rectangle.h - Testat/Scene.cpp - Testat/Scene.h - ../../helpers/AnsiConsole.cpp + Testat/main_mp4_OO_b.cpp ) \ No newline at end of file diff --git a/src/05_OO/b/MP/Circle.cpp b/src/05_OO/b/MP/Circle.cpp deleted file mode 100644 index 5ef58f7..0000000 --- a/src/05_OO/b/MP/Circle.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "Circle.h" -#include - -Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { - _radius = radius; -} - -void Circle::draw() { - /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie - * Höhensatz des Euklid - * */ - int x_start = position.x - _radius / 2; - int x_stop = position.x + _radius / 2; - - for (int i = x_start; i <= x_stop; i++) { - double x_relative = double(i) - double(x_start); - double h = sqrt(x_relative * (x_stop - x_start - x_relative)); - ansiConsole.printText(position.x + int(x_relative) - _radius / 2, - static_cast(position.y + h), "#", - color); - ansiConsole.printText(position.x + int(x_relative) - _radius / 2, - static_cast(position.y - h), "#", - color); - - } -} diff --git a/src/05_OO/b/MP/Circle.h b/src/05_OO/b/MP/Circle.h deleted file mode 100644 index f0186d2..0000000 --- a/src/05_OO/b/MP/Circle.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_CIRCLE_H -#define C_C_CIRCLE_H - -#include "Shape.h" - -class Circle : public Shape{ -protected: - int _radius; -public: - explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); - void draw() override; -}; - -#endif //C_C_CIRCLE_H diff --git a/src/05_OO/b/MP/Point.cpp b/src/05_OO/b/MP/Point.cpp deleted file mode 100644 index 77efe5c..0000000 --- a/src/05_OO/b/MP/Point.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Point.h" - -Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { -} - -void Point::draw() { - ansiConsole.printText(position.x, position.y, "*", color); -} diff --git a/src/05_OO/b/MP/Point.h b/src/05_OO/b/MP/Point.h deleted file mode 100644 index bc693bf..0000000 --- a/src/05_OO/b/MP/Point.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_POINT_H -#define C_C_POINT_H - -#include "Shape.h" - -class Point : public Shape{ - -public: - explicit Point(int x, int y, Colors color); - void draw() override; -}; - - -#endif diff --git a/src/05_OO/b/MP/Position.h b/src/05_OO/b/MP/Position.h deleted file mode 100644 index f977d98..0000000 --- a/src/05_OO/b/MP/Position.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_POSITION_H -#define C_C_POSITION_H - -struct Position { - int x; - int y; - - explicit Position(int x_ = 0, int y_ = 0) { - x = x_; - y = y_; - } -}; - -#endif \ No newline at end of file diff --git a/src/05_OO/b/MP/Rectangle.cpp b/src/05_OO/b/MP/Rectangle.cpp deleted file mode 100644 index 7aaa71a..0000000 --- a/src/05_OO/b/MP/Rectangle.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "Rectangle.h" - -Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { - this->width = width; - this->height = height; -} - -void Rectangle::draw() { - int x_start = position.x - (width / 2); - int x_stop = position.x + (width / 2); - - int y_start = position.y - (height / 2); - int y_stop = position.y + (height / 2); - - for(int i = x_start; i <= x_stop; i++) { - ansiConsole.printText(i, position.y - (height / 2), "#", color); - ansiConsole.printText(i, position.y + (height / 2), "#", color); - } - for(int i = y_start; i< y_stop; i++) { - ansiConsole.printText(position.x + (width / 2), i, "#", color); - ansiConsole.printText(position.x - (width / 2), i, "#", color); - } -} \ No newline at end of file diff --git a/src/05_OO/b/MP/Rectangle.h b/src/05_OO/b/MP/Rectangle.h deleted file mode 100644 index f8775b9..0000000 --- a/src/05_OO/b/MP/Rectangle.h +++ /dev/null @@ -1,18 +0,0 @@ -#include "Shape.h" - -#ifndef C_C_RECTANGLE_H -#define C_C_RECTANGLE_H - - -class Rectangle : public Shape{ -protected: - int width; - int height; - -public: - explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); - void draw() override; -}; - - -#endif //C_C_RECTANGLE_H diff --git a/src/05_OO/b/MP/Scene.cpp b/src/05_OO/b/MP/Scene.cpp deleted file mode 100644 index 9d25e7e..0000000 --- a/src/05_OO/b/MP/Scene.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Scene.h" -#include - -Scene::Scene(std::vector shapes) : shapes(std::move(shapes)) { - -} - -void Scene::draw() { - ansiConsole.clearScreen(); - for(Shape* shape : shapes) { - shape->draw(); - } -} - -Scene::~Scene() { - for(Shape* shape : shapes) { - delete shape; - } -} diff --git a/src/05_OO/b/MP/Scene.h b/src/05_OO/b/MP/Scene.h deleted file mode 100644 index 90e3352..0000000 --- a/src/05_OO/b/MP/Scene.h +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include "Shape.h" - -#ifndef C_C_SCENE_H -#define C_C_SCENE_H - -class Scene { -private: - std::vector shapes; -public: - explicit Scene(std::vector shapes); - void draw(); - ~Scene(); -}; - - -#endif //C_C_SCENE_H diff --git a/src/05_OO/b/MP/Shape.cpp b/src/05_OO/b/MP/Shape.cpp deleted file mode 100644 index 6fa576c..0000000 --- a/src/05_OO/b/MP/Shape.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Position.h" -#include "Shape.h" - -Shape::Shape(Position position, Colors color) { - this->position = position; - this->color = color; -} - -Shape::~Shape() = default; \ No newline at end of file diff --git a/src/05_OO/b/MP/Shape.h b/src/05_OO/b/MP/Shape.h deleted file mode 100644 index f354055..0000000 --- a/src/05_OO/b/MP/Shape.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef C_C_SHAPE_H -#define C_C_SHAPE_H - -#include "../../../helpers/AnsiConsole.h" -#include "Position.h" - -class Shape { -protected: - Position position; - Colors color; -public: - Shape(Position position, Colors color); - virtual ~Shape(); - virtual void draw() = 0; -}; - -#endif \ No newline at end of file diff --git a/src/05_OO/b/MP/main_mp4_OO_b.cpp b/src/05_OO/b/MP/main_mp4_OO_b.cpp new file mode 100644 index 0000000..8eda322 --- /dev/null +++ b/src/05_OO/b/MP/main_mp4_OO_b.cpp @@ -0,0 +1,220 @@ +// file main_mp4_OO_b.cpp +#include +#include + +//======================================= + +struct FooVal { + FooVal(int initialValue=0); + ~FooVal(); +private: + int _someValue; +}; + +//======================================= + +class FooBase { + FooVal _value; +public: + FooBase(); + FooBase(int initialValue); + virtual ~FooBase(); +}; + +//======================================= + +class FooDerived : public FooBase { + int _ival; +public: + FooDerived(int initialValue); + ~FooDerived(); +}; + +//======================================= + +class Bar { + FooBase *_helperObject; +public: + Bar(); + ~Bar(); +}; + +//======================================= + +FooVal::FooVal(int initialValue) +: _someValue(initialValue) +{ + std::cout << "FooVal::FooVal()" << std::endl; +} + +FooVal::~FooVal(){ + std::cout << "FooVal::~FooVal()" << std::endl; +} + +FooBase::FooBase(){ + std::cout << "FooBase::FooBase()" << std::endl; +} + +FooBase::FooBase(int initialValue) +: _value(initialValue) +{ + std::cout << "FooBase::FooBase(int)" << std::endl; +} + +FooBase::~FooBase(){ + std::cout << "FooBase::~FooBase(" << std::endl; +} + +FooDerived::FooDerived(int initialValue) +: FooBase(initialValue), _ival(0) +{ + std::cout << "FooDerived::FooDerived()" << std::endl; +} + +FooDerived::~FooDerived(){ + std::cout << "FooDerived::~FooDerived()" << std::endl; +} + +Bar::Bar(){ + _helperObject = new FooDerived(0); +} + +Bar::~Bar(){ + delete _helperObject; +} + + +struct StackObject { +private: + void* operator new(size_t size) noexcept { + bool noStackObjectOnHeap = false; + assert(noStackObjectOnHeap); + return nullptr; + } +}; + + +struct A : public StackObject { + A(){std::cout << "+A ";} + //A(const A&){std::cout << "+A";} + ~A(){std::cout << "-A ";} +}; + +struct B : public StackObject { + B(){std::cout << "+B ";} + //B(const B&){std::cout << "+B";} + ~B(){std::cout << "-B ";} +}; + + +struct C : public StackObject { + C(){std::cout << "+C ";} + //C(const C&){std::cout << "+C";} + ~C(){std::cout << "-C ";} +}; + +class HeapObject{ +public: + void* operator new (size_t size); + HeapObject(); + virtual ~HeapObject(); + static bool assertionsHold(); +protected: +private: + static int ctorCount; + static int dtorCount; + static int newCount; + // static void remove(HeapObject *); + HeapObject(const HeapObject&) = delete; + HeapObject& operator=(const HeapObject&) = delete; +}; + +int HeapObject::ctorCount = 0; +int HeapObject::dtorCount = 0; +int HeapObject::newCount = 0; + +void* HeapObject::operator new (size_t size){ + newCount++; + return new char[size]; +} + +HeapObject::HeapObject(){ + ctorCount++; +} + +HeapObject::~HeapObject(){ + dtorCount++; +} + +bool HeapObject::assertionsHold(){ + assert(ctorCount == newCount); // all objects have been allocated on heap + assert(ctorCount == dtorCount); // all objects have been deleted + return true; +} + + +class K : public HeapObject +{ +public: + K(){std::cout << "+K ";} + ~K(){std::cout << "-K ";} +}; + +class L { +public: + L(){std::cout << "+L ";} + ~L(){std::cout << "-L ";} +}; + + +class M { +public: + M(){std::cout << "+M ";} + ~M(){std::cout << "-M ";} +}; + +void pattern1() { + B b; + { + C c; + } + A a; +} + +void pattern2() { + B b; + C c; + A a; + +} + + + + +int main(int argc, const char * argv[]) { + + Bar * bar = new Bar(); + // ... + delete bar; // implies "delete _helperObject"; + + FooBase *obj = new FooDerived(7); + delete obj; + + { + C c; + K* k = new K(); + delete k; + } + + HeapObject::assertionsHold(); + std::cout << " ENDE" << std::endl; + + pattern1(); + std::cout << std::endl; + pattern2(); + + return 0; +} + + + diff --git a/src/05_OO/b/MP/shapes_main.cpp b/src/05_OO/b/MP/shapes_main.cpp deleted file mode 100644 index 20dbcde..0000000 --- a/src/05_OO/b/MP/shapes_main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "Shape.h" -#include "Point.h" -#include "Circle.h" -#include "Rectangle.h" -#include "Scene.h" - -int main(int argc, char **argv) { - std::vector shapes; - - shapes.push_back(new Point(30, 10, Colors::RED)); - shapes.push_back(new Point(28, 8, Colors::BLUE)); - shapes.push_back(new Point(32, 8, Colors::BLUE)); - - - shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); - shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); - - - shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); - - shapes.push_back(new Point(5, 10, Colors::WHITE)); - 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; - - - return 0; -} diff --git a/src/05_OO/b/Testat/Circle.cpp b/src/05_OO/b/Testat/Circle.cpp deleted file mode 100644 index 5ef58f7..0000000 --- a/src/05_OO/b/Testat/Circle.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "Circle.h" -#include - -Circle::Circle(int x, int y, int radius, Colors color) : Shape(Position(x, y), color) { - _radius = radius; -} - -void Circle::draw() { - /* see https://de.wikibooks.org/wiki/Formelsammlung_Mathematik:_Geometrie - * Höhensatz des Euklid - * */ - int x_start = position.x - _radius / 2; - int x_stop = position.x + _radius / 2; - - for (int i = x_start; i <= x_stop; i++) { - double x_relative = double(i) - double(x_start); - double h = sqrt(x_relative * (x_stop - x_start - x_relative)); - ansiConsole.printText(position.x + int(x_relative) - _radius / 2, - static_cast(position.y + h), "#", - color); - ansiConsole.printText(position.x + int(x_relative) - _radius / 2, - static_cast(position.y - h), "#", - color); - - } -} diff --git a/src/05_OO/b/Testat/Circle.h b/src/05_OO/b/Testat/Circle.h deleted file mode 100644 index f0186d2..0000000 --- a/src/05_OO/b/Testat/Circle.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_CIRCLE_H -#define C_C_CIRCLE_H - -#include "Shape.h" - -class Circle : public Shape{ -protected: - int _radius; -public: - explicit Circle(int x = 0, int y = 0, int radius = 0, Colors color = Colors::GREEN); - void draw() override; -}; - -#endif //C_C_CIRCLE_H diff --git a/src/05_OO/b/Testat/Point.cpp b/src/05_OO/b/Testat/Point.cpp deleted file mode 100644 index 77efe5c..0000000 --- a/src/05_OO/b/Testat/Point.cpp +++ /dev/null @@ -1,8 +0,0 @@ -#include "Point.h" - -Point::Point(int x, int y, Colors color) : Shape(Position(x, y), color) { -} - -void Point::draw() { - ansiConsole.printText(position.x, position.y, "*", color); -} diff --git a/src/05_OO/b/Testat/Point.h b/src/05_OO/b/Testat/Point.h deleted file mode 100644 index bc693bf..0000000 --- a/src/05_OO/b/Testat/Point.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_POINT_H -#define C_C_POINT_H - -#include "Shape.h" - -class Point : public Shape{ - -public: - explicit Point(int x, int y, Colors color); - void draw() override; -}; - - -#endif diff --git a/src/05_OO/b/Testat/Position.h b/src/05_OO/b/Testat/Position.h deleted file mode 100644 index f977d98..0000000 --- a/src/05_OO/b/Testat/Position.h +++ /dev/null @@ -1,14 +0,0 @@ -#ifndef C_C_POSITION_H -#define C_C_POSITION_H - -struct Position { - int x; - int y; - - explicit Position(int x_ = 0, int y_ = 0) { - x = x_; - y = y_; - } -}; - -#endif \ No newline at end of file diff --git a/src/05_OO/b/Testat/Rectangle.cpp b/src/05_OO/b/Testat/Rectangle.cpp deleted file mode 100644 index 7aaa71a..0000000 --- a/src/05_OO/b/Testat/Rectangle.cpp +++ /dev/null @@ -1,23 +0,0 @@ -#include "Rectangle.h" - -Rectangle::Rectangle(int x, int y, int width, int height, Colors color) : Shape(Position(x, y), color) { - this->width = width; - this->height = height; -} - -void Rectangle::draw() { - int x_start = position.x - (width / 2); - int x_stop = position.x + (width / 2); - - int y_start = position.y - (height / 2); - int y_stop = position.y + (height / 2); - - for(int i = x_start; i <= x_stop; i++) { - ansiConsole.printText(i, position.y - (height / 2), "#", color); - ansiConsole.printText(i, position.y + (height / 2), "#", color); - } - for(int i = y_start; i< y_stop; i++) { - ansiConsole.printText(position.x + (width / 2), i, "#", color); - ansiConsole.printText(position.x - (width / 2), i, "#", color); - } -} \ No newline at end of file diff --git a/src/05_OO/b/Testat/Rectangle.h b/src/05_OO/b/Testat/Rectangle.h deleted file mode 100644 index f8775b9..0000000 --- a/src/05_OO/b/Testat/Rectangle.h +++ /dev/null @@ -1,18 +0,0 @@ -#include "Shape.h" - -#ifndef C_C_RECTANGLE_H -#define C_C_RECTANGLE_H - - -class Rectangle : public Shape{ -protected: - int width; - int height; - -public: - explicit Rectangle(int x = 0, int y = 0, int width = 0, int height = 0, Colors color = Colors::WHITE); - void draw() override; -}; - - -#endif //C_C_RECTANGLE_H diff --git a/src/05_OO/b/Testat/Scene.cpp b/src/05_OO/b/Testat/Scene.cpp deleted file mode 100644 index 9d25e7e..0000000 --- a/src/05_OO/b/Testat/Scene.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "Scene.h" -#include - -Scene::Scene(std::vector shapes) : shapes(std::move(shapes)) { - -} - -void Scene::draw() { - ansiConsole.clearScreen(); - for(Shape* shape : shapes) { - shape->draw(); - } -} - -Scene::~Scene() { - for(Shape* shape : shapes) { - delete shape; - } -} diff --git a/src/05_OO/b/Testat/Scene.h b/src/05_OO/b/Testat/Scene.h deleted file mode 100644 index 90e3352..0000000 --- a/src/05_OO/b/Testat/Scene.h +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include "Shape.h" - -#ifndef C_C_SCENE_H -#define C_C_SCENE_H - -class Scene { -private: - std::vector shapes; -public: - explicit Scene(std::vector shapes); - void draw(); - ~Scene(); -}; - - -#endif //C_C_SCENE_H diff --git a/src/05_OO/b/Testat/Shape.cpp b/src/05_OO/b/Testat/Shape.cpp deleted file mode 100644 index 6fa576c..0000000 --- a/src/05_OO/b/Testat/Shape.cpp +++ /dev/null @@ -1,9 +0,0 @@ -#include "Position.h" -#include "Shape.h" - -Shape::Shape(Position position, Colors color) { - this->position = position; - this->color = color; -} - -Shape::~Shape() = default; \ No newline at end of file diff --git a/src/05_OO/b/Testat/Shape.h b/src/05_OO/b/Testat/Shape.h deleted file mode 100644 index f354055..0000000 --- a/src/05_OO/b/Testat/Shape.h +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef C_C_SHAPE_H -#define C_C_SHAPE_H - -#include "../../../helpers/AnsiConsole.h" -#include "Position.h" - -class Shape { -protected: - Position position; - Colors color; -public: - Shape(Position position, Colors color); - virtual ~Shape(); - virtual void draw() = 0; -}; - -#endif \ No newline at end of file diff --git a/src/05_OO/b/Testat/main_mp4_OO_b.cpp b/src/05_OO/b/Testat/main_mp4_OO_b.cpp new file mode 100644 index 0000000..25160e8 --- /dev/null +++ b/src/05_OO/b/Testat/main_mp4_OO_b.cpp @@ -0,0 +1,198 @@ +// file main_mp4_OO_b.cpp +#include +#include + +//======================================= + +struct FooVal { + FooVal(int initialValue=0); + ~FooVal(); +private: + int _someValue; +}; + +//======================================= + +class FooBase { + FooVal _value; +public: + FooBase(); + FooBase(int initialValue); + virtual ~FooBase(); +}; + +//======================================= + +class FooDerived : public FooBase { + int _ival; +public: + FooDerived(int initialValue); + ~FooDerived(); +}; + +//======================================= + +class Bar { + FooBase *_helperObject; +public: + Bar(); + ~Bar(); +}; + +//======================================= + +FooVal::FooVal(int initialValue) +: _someValue(initialValue) +{ + std::cout << "FooVal::FooVal()" << std::endl; +} + +FooVal::~FooVal(){ + std::cout << "FooVal::~FooVal()" << std::endl; +} + +FooBase::FooBase(){ + std::cout << "FooBase::FooBase()" << std::endl; +} + +FooBase::FooBase(int initialValue) +: _value(initialValue) +{ + std::cout << "FooBase::FooBase(int)" << std::endl; +} + +FooBase::~FooBase(){ + std::cout << "FooBase::~FooBase(" << std::endl; +} + +FooDerived::FooDerived(int initialValue) +: FooBase(initialValue), _ival(0) +{ + std::cout << "FooDerived::FooDerived()" << std::endl; +} + +FooDerived::~FooDerived(){ + std::cout << "FooDerived::~FooDerived()" << std::endl; +} + +Bar::Bar(){ + _helperObject = new FooDerived(0); +} + +Bar::~Bar(){ + delete _helperObject; +} + + +struct StackObject { +private: + void* operator new(size_t size) noexcept { + bool noStackObjectOnHeap = false; + assert(noStackObjectOnHeap); + return nullptr; + } +}; + + +struct A : public StackObject { + A(){std::cout << "+A ";} + //A(const A&){std::cout << "+A";} + ~A(){std::cout << "-A ";} +}; + +struct B : public StackObject { + B(){std::cout << "+B ";} + //B(const B&){std::cout << "+B";} + ~B(){std::cout << "-B ";} +}; + + +struct C : public StackObject { + C(){std::cout << "+C ";} + //C(const C&){std::cout << "+C";} + ~C(){std::cout << "-C ";} +}; + +class HeapObject{ +public: + void* operator new (size_t size); + HeapObject(); + virtual ~HeapObject(); + static bool assertionsHold(); +protected: +private: + static int ctorCount; + static int dtorCount; + static int newCount; + // static void remove(HeapObject *); + HeapObject(const HeapObject&) = delete; + HeapObject& operator=(const HeapObject&) = delete; +}; + +int HeapObject::ctorCount = 0; +int HeapObject::dtorCount = 0; +int HeapObject::newCount = 0; + +void* HeapObject::operator new (size_t size){ + newCount++; + return new char[size]; +} + +HeapObject::HeapObject(){ + ctorCount++; +} + +HeapObject::~HeapObject(){ + dtorCount++; +} + +bool HeapObject::assertionsHold(){ + assert(ctorCount == newCount); // all objects have been allocated on heap + assert(ctorCount == dtorCount); // all objects have been deleted + return true; +} + + +class K : public HeapObject +{ +public: + K(){std::cout << "+K ";} + ~K(){std::cout << "-K ";} +}; + +class L { +public: + L(){std::cout << "+L ";} + ~L(){std::cout << "-L ";} +}; + + +class M { +public: + M(){std::cout << "+M ";} + ~M(){std::cout << "-M ";} +}; + + + +int main(int argc, const char * argv[]) { + Bar * bar = new Bar(); + // ... + delete bar; // implies "delete _helperObject"; + + FooBase *obj = new FooDerived(7); + delete obj; + + { + C c; + K* k = new K(); + delete k; + } + + HeapObject::assertionsHold(); + std::cout << " ENDE" << std::endl; + return 0; +} + + + diff --git a/src/05_OO/b/Testat/shapes_main.cpp b/src/05_OO/b/Testat/shapes_main.cpp deleted file mode 100644 index 20dbcde..0000000 --- a/src/05_OO/b/Testat/shapes_main.cpp +++ /dev/null @@ -1,34 +0,0 @@ -#include -#include "Shape.h" -#include "Point.h" -#include "Circle.h" -#include "Rectangle.h" -#include "Scene.h" - -int main(int argc, char **argv) { - std::vector shapes; - - shapes.push_back(new Point(30, 10, Colors::RED)); - shapes.push_back(new Point(28, 8, Colors::BLUE)); - shapes.push_back(new Point(32, 8, Colors::BLUE)); - - - shapes.push_back(new Circle(30, 10, 10, Colors::WHITE)); - shapes.push_back(new Circle(30, 20, 15, Colors::GREEN)); - - - shapes.push_back(new Rectangle(5, 21, 10, 10, Colors::MAGENTA)); - - shapes.push_back(new Point(5, 10, Colors::WHITE)); - 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; - - - return 0; -}