consolidate all repos to one for archive
This commit is contained in:
23
semester_2/programiranje_2/primeri/Example14/Circle.h
Normal file
23
semester_2/programiranje_2/primeri/Example14/Circle.h
Normal 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
|
33
semester_2/programiranje_2/primeri/Example14/Composite.cpp
Normal file
33
semester_2/programiranje_2/primeri/Example14/Composite.cpp
Normal 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();
|
||||
}
|
17
semester_2/programiranje_2/primeri/Example14/Composite.h
Normal file
17
semester_2/programiranje_2/primeri/Example14/Composite.h
Normal 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
|
21
semester_2/programiranje_2/primeri/Example14/Rectangle.h
Normal file
21
semester_2/programiranje_2/primeri/Example14/Rectangle.h
Normal 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
|
26
semester_2/programiranje_2/primeri/Example14/Shape.h
Normal file
26
semester_2/programiranje_2/primeri/Example14/Shape.h
Normal 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
|
36
semester_2/programiranje_2/primeri/Example14/main.cpp
Normal file
36
semester_2/programiranje_2/primeri/Example14/main.cpp
Normal 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;
|
||||
}
|
Reference in New Issue
Block a user