-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-class-inheritance.cpp
More file actions
289 lines (228 loc) · 5.86 KB
/
2-class-inheritance.cpp
File metadata and controls
289 lines (228 loc) · 5.86 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* parent class
*/
class Object {
int m_color[3];
int m_type;
int m_size;
int m_x;
int m_y;
public:
enum Type_e {
NONE = 0,
RECTANGLE,
CIRCLE,
ELIPSE,
NUM_TYPES,
};
// inicializacni konstruktor
Object() {
m_type = 0;
m_size = 0;
m_x = 0;
m_y = 0;
m_color[0] = m_color[1] = m_color[2] = 255;
}
// pretezovani konstruktoru
Object(int type) : Object() {
m_type = type;
}
Object(int type, int size) : Object(type) {
m_size = size;
}
Object(int type, int x, int y) : Object(type) {
m_x = x;
m_y = y;
}
Object(int type, int size, int x, int y) : Object(type, size) {
m_x = x;
m_y = y;
}
Object(Object *copy) {
if (copy == NULL) return;
m_type = copy->getType();
m_size = copy->getSize();
m_x = copy->m_x;
m_y = copy->m_y;
m_color[0] = copy->m_color[0];
m_color[1] = copy->m_color[1];
m_color[2] = copy->m_color[2];
}
virtual ~Object() {
}
int getType() {
return m_type;
}
const char *getTypeName() {
const char *ret;
switch(m_type) {
case NONE: ret = "None"; break;
case RECTANGLE: ret = "Rectangle"; break;
case CIRCLE: ret = "Circle"; break;
case ELIPSE: ret = "Elipse"; break;
default:
ret = "Unknown type!";
break;
}
return ret;
}
void setSize(int size) {
m_size = size;
}
int getSize() {
return m_size;
}
void setPosition(int x, int y) {
m_x = x;
m_y = y;
}
int getX() {
return m_x;
}
int getY() {
return m_y;
}
void setColor(int *color) {
m_color[0] = color[0];
m_color[1] = color[1];
m_color[2] = color[2];
}
// pretezovani setColor
void setColor(int r, int g, int b) {
m_color[0] = r;
m_color[1] = g;
m_color[2] = b;
}
int *getColor() {
return m_color;
}
virtual void render() {
}
};
/* child class : Object
*/
class Rectangle : public Object {
public:
Rectangle() : Object(RECTANGLE) {
}
Rectangle(int x, int y) : Object(RECTANGLE, x, y) {
}
~Rectangle() {
}
virtual void render() {
}
};
/* child class : Object
*/
class Circle : public Object {
public:
Circle() : Object(CIRCLE) {
}
~Circle() {
}
virtual void render() {
}
};
/* child class : Object
*/
class Elipse : public Object {
public:
Elipse() : Object(ELIPSE) {
}
~Elipse() {
}
virtual void render() {
}
};
/* For now unknown type
*/
class Hexagon : public Object {
public:
Hexagon() : Object() {
}
~Hexagon() {
}
virtual void render() {
}
};
/*
*/
void printObjectInfo(Object *object) {
if (object == NULL) return;
int *color = object->getColor();
printf("printObjectInfo():\n\ttype: %d\n\ttypename: %s\n\tsize: %d\n\tx: %d\ty: %d\n\tr: %d\tg: %d\tb: %d\n",
object->getType(),
object->getTypeName(),
object->getSize(),
object->getX(), object->getY(),
color[0], color[1], color[2]);
}
/*
*/
class ObjectManager {
ObjectManager() {
}
~ObjectManager() {
}
public:
static Object *createObject() {
return new Object();
}
static Rectangle *createRectangle() {
return new Rectangle();
}
static Elipse *createElipse() {
return new Elipse();
}
static Circle *createCircle() {
return new Circle();
}
};
/*
*/
int main() {
srand(time(NULL)); // inicializace random number generatoru
Object *primitives[6];
int size = sizeof(primitives)/sizeof(*primitives);
// init
for (int i = 0; i < size; i++) {
// random object creation
int random_type = (rand() % (0 - Object::NUM_TYPES) + 0);
switch(random_type) {
default: primitives[i] = ObjectManager::createObject(); break;
case Object::CIRCLE: primitives[i] = ObjectManager::createCircle(); break;
case Object::ELIPSE: primitives[i] = ObjectManager::createElipse(); break;
case Object::RECTANGLE: primitives[i] = ObjectManager::createRectangle(); break;
}
}
int color[3] = { 244, 53 , 22};
primitives[2]->setColor(color);
// print all primitive info
for (int i = 0; i < size; i++) {
printObjectInfo(primitives[i]);
}
// memory safe release
for (int i = 0; i < size; i++) {
delete primitives[i];
primitives[i] = nullptr;
}
// constructor copy
Rectangle *rect = new Rectangle();
printObjectInfo(rect);
// create copy from another object
Object *copy_rect_1 = new Object(rect);
delete rect;
rect = nullptr;
printObjectInfo(copy_rect_1);
// create copy from ObjectManager class
Object *copy_rect_2 = new Object(ObjectManager::createRectangle());
printObjectInfo(copy_rect_2);
delete copy_rect_1;
copy_rect_1 = nullptr;
delete copy_rect_2;
copy_rect_2 = nullptr;
return 0;
}