05_OOb: Anfang Bearbeitung MP

This commit is contained in:
Johannes Theiner 2018-11-09 21:43:48 +01:00
parent 72b7ae9a7d
commit 8d01cff127
27 changed files with 420 additions and 452 deletions

View File

@ -1,31 +1,7 @@
add_executable(05_OOb_MP add_executable(05_OOb_MP
MP/shapes_main.cpp MP/main_mp4_OO_b.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
) )
add_executable(05_OOb_Testat add_executable(05_OOb_Testat
Testat/shapes_main.cpp Testat/main_mp4_OO_b.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
) )

View File

@ -1,26 +0,0 @@
#include "Circle.h"
#include <cmath>
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<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y - h), "#",
color);
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -1,19 +0,0 @@
#include "Scene.h"
#include <vector>
Scene::Scene(std::vector<Shape*> shapes) : shapes(std::move(shapes)) {
}
void Scene::draw() {
ansiConsole.clearScreen();
for(Shape* shape : shapes) {
shape->draw();
}
}
Scene::~Scene() {
for(Shape* shape : shapes) {
delete shape;
}
}

View File

@ -1,17 +0,0 @@
#include <vector>
#include "Shape.h"
#ifndef C_C_SCENE_H
#define C_C_SCENE_H
class Scene {
private:
std::vector<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
#endif //C_C_SCENE_H

View File

@ -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;

View File

@ -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

View File

@ -0,0 +1,220 @@
// file main_mp4_OO_b.cpp
#include <iostream>
#include <cassert>
//=======================================
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;
}

View File

@ -1,34 +0,0 @@
#include <vector>
#include "Shape.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Scene.h"
int main(int argc, char **argv) {
std::vector<Shape*> 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;
}

View File

@ -1,26 +0,0 @@
#include "Circle.h"
#include <cmath>
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<int>(position.y + h), "#",
color);
ansiConsole.printText(position.x + int(x_relative) - _radius / 2,
static_cast<int>(position.y - h), "#",
color);
}
}

View File

@ -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

View File

@ -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);
}

View File

@ -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

View File

@ -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

View File

@ -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);
}
}

View File

@ -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

View File

@ -1,19 +0,0 @@
#include "Scene.h"
#include <vector>
Scene::Scene(std::vector<Shape*> shapes) : shapes(std::move(shapes)) {
}
void Scene::draw() {
ansiConsole.clearScreen();
for(Shape* shape : shapes) {
shape->draw();
}
}
Scene::~Scene() {
for(Shape* shape : shapes) {
delete shape;
}
}

View File

@ -1,17 +0,0 @@
#include <vector>
#include "Shape.h"
#ifndef C_C_SCENE_H
#define C_C_SCENE_H
class Scene {
private:
std::vector<Shape*> shapes;
public:
explicit Scene(std::vector<Shape*> shapes);
void draw();
~Scene();
};
#endif //C_C_SCENE_H

View File

@ -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;

View File

@ -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

View File

@ -0,0 +1,198 @@
// file main_mp4_OO_b.cpp
#include <iostream>
#include <cassert>
//=======================================
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;
}

View File

@ -1,34 +0,0 @@
#include <vector>
#include "Shape.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Scene.h"
int main(int argc, char **argv) {
std::vector<Shape*> 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;
}