consolidate all repos to one for archive

This commit is contained in:
2025-01-28 13:46:42 +01:00
commit a6610fbc7a
5350 changed files with 2705721 additions and 0 deletions

View File

@@ -0,0 +1,23 @@
#ifndef EXAMPLE14_CIRCLE_H
#define EXAMPLE14_CIRCLE_H
#include <iostream>
class Circle : public Shape {
private:
int r;
public:
Circle() : Shape(), r(0) {
}
Circle(int x, int y, int r) : Shape(x, y) , r(r) {
}
double area() const {
return 3.14*r*r;
}
void print() const {
std::cout << "Circle(" << x << ", " << y << ", " << r << ")" << std::endl;
}
};
#endif //EXAMPLE14_CIRCLE_H

View File

@@ -0,0 +1,33 @@
#include "Shape.h"
#include "Composite.h"
Composite::Composite(Shape* s) : Shape(), ptrShape(s), ptrNext(nullptr) {
}
void Composite::add(Shape* s) {
if (!ptrShape) ptrShape = s;
else {
if (!ptrNext) ptrNext = new Composite(s);
else ptrNext->add(s);
}
}
double Composite::area() const {
if (!ptrNext) return ptrShape->area();
else return ptrShape->area() + ptrNext->area();
}
void Composite::print() const {
if (ptrShape) ptrShape->print();
if (ptrNext) ptrNext->print();
}
void Composite::relMove(int x1, int y1) {
if (ptrShape) ptrShape->relMove(x1, y1);
if (ptrNext) ptrNext->relMove(x1, y1);
}
void Composite::deleteRec() {
if (ptrShape) delete ptrShape;
if (ptrNext) ptrNext->deleteRec();
}

View File

@@ -0,0 +1,17 @@
#ifndef EXAMPLE14_COMPOSITE_H
#define EXAMPLE14_COMPOSITE_H
class Composite : public Shape {
private:
Shape* ptrShape;
Composite* ptrNext;
public:
Composite(Shape* s = nullptr);
void add(Shape* s);
double area() const;
void print() const;
void relMove(int x1, int y1);
void deleteRec();
};
#endif //EXAMPLE14_COMPOSITE_H

View File

@@ -0,0 +1,21 @@
#ifndef EXAMPLE14_RECTANGLE_H
#define EXAMPLE14_RECTANGLE_H
#include <iostream>
class Rectangle : public Shape {
private:
int w, h;
public:
Rectangle() : Shape(), w(0), h(0) {
}
Rectangle(int x, int y, int w, int h) : Shape(x, y), w(w), h(h) {
}
double area() const {
return w*h;
}
void print() const {
std::cout << "Rectangle(" << x << ", " << y << ", " <<
w << ", " << h << ")" << std::endl;}
};
#endif //EXAMPLE14_RECTANGLE_H

View File

@@ -0,0 +1,26 @@
#ifndef EXAMPLE14_SHAPE_H
#define EXAMPLE14_SHAPE_H
#include <iostream>
class Shape { // abstract class
protected:
int x,y;
public:
Shape() : x(0), y(0) {}
Shape(int x, int y) : x(x), y(y) {}
virtual double area() const = 0; //abstract constant method
virtual void print() const = 0; //abstract constant method
virtual void relMove(int dx, int dy) {
x+=dx;
y+=dy;
}
};
#endif //EXAMPLE14_SHAPE_H

View File

@@ -0,0 +1,36 @@
#include <iostream>
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Composite.h"
int main() {
Rectangle r1(0,0,10,10);
Circle c1(0, 0, 10);
r1.print();
c1.print();
std::cout << "area of a circle = " << c1.area() << std::endl;
std::cout << "area of a rectangle = " << r1.area() << std::endl;
/*
std::cout << "---------------" << std::endl;
Composite c;
c.add(new Rectangle(1,1,1,10));
c.add(new Circle(1,1,1));
c.print();
std::cout << "area of a composite = " << c.area() << std::endl;
std::cout << "---------------" << std::endl;
Composite cc;
cc.add(new Circle(2,2,2));
cc.add(&c);
cc.print();
std::cout << "area of a composite = " << cc.area() << std::endl; //3.14*2*2 + 1*10 + 3.14*1*1
std::cout << "---------------" << std::endl;
cc.relMove(10,10);
cc.print();
*/
//c.deleteRec();
//cc.deleteRec();
return 0;
}