Website Summary Files Commits Branches @commit @diff @stats
path: root / Polymorphism / ShapeCollection.cpp
blob: f2c27f224eda5c97aeb8079a3f1585b41aeebcca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "ShapeCollection.h"

ShapeCollection::ShapeCollection()
{
	count = 0;
	capacity = 8;

	data = new Shape*[capacity];
}

void ShapeCollection::AddCircle(int x1, int y1, int r)
{
	Circle* current = new Circle(x1, y1, r);
	Add(current);

}
void ShapeCollection::AddTriagnle(int x1, int y1, int x2, int y2, int x3, int y3)
{
	Triangle* current = new Triangle(x1, y1, x2, y2, x3, y3);
	Add(current);
}
void ShapeCollection::AddRect(int x1, int y1, int x2, int y2)
{
	Rectangle* current = new Rectangle(x1, y1, x2, y2);
	Add(current);
}
void ShapeCollection::Add(Shape* currentShape)
{
	if (count == capacity)
		Resize();
	data[count++] = currentShape;
}
void ShapeCollection::Free()
{
	for (int i = 0; i < count; i++)
		delete data[i];

	delete[] data;
}
void ShapeCollection::CopyFrom(const ShapeCollection& other)
{
	data = new Shape*[other.count];
	capacity = other.capacity;
	count = other.count;
	for (int i = 0; i < other.count; i++)
		data[i] = other.data[i]->clone();

}
ShapeCollection::ShapeCollection(const ShapeCollection& other)
{
	CopyFrom(other);
}
ShapeCollection& ShapeCollection::operator = (const ShapeCollection& other)
{
	if (this != &other)
	{
		Free();
		CopyFrom(other);
	}
	return *this;
}
ShapeCollection::~ShapeCollection()
{
	Free();
}
void ShapeCollection::Resize()
{
	Shape** temp = new Shape*[capacity * 2];
	capacity *= 2;
	for (int i = 0; i < count; i++)
		temp[i] = data[i];
	delete[] data;
	data = temp;


}
void ShapeCollection::PrintAllAreas()
{
	for (int i = 0; i < count; i++)
		std::cout << data[i]->getArea() << " ";
	std::cout<<std::endl;
}
void ShapeCollection::PrintAllPers()
{
	for (int i = 0; i < count; i++)
		std::cout << data[i]->getPer() << " ";
	std::cout << std::endl;
}