-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrototype.cpp
More file actions
85 lines (79 loc) · 1.96 KB
/
Prototype.cpp
File metadata and controls
85 lines (79 loc) · 1.96 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
//
// Created by osher on 14/02/2022.
//
#include <iostream>
#include <functional>
using namespace std;
class Shape {
protected:
const char* _shapeName;
public:
Shape(const char* name):_shapeName(name){}
virtual Shape* clone() const=0;
virtual void print()=0;
};
class Circle : public Shape{
int _x, _y;
double _radius;
public:
Circle(int x, int y, double r): Shape("circle") {
_x = x; _y = y; _radius = r;
}
Circle(): Shape("circle") {
_x = 0; _y = 0; _radius = 0;
}
Shape* clone() const override {
return (Shape*)new Circle(_x, _y, _radius);
};
void print() {
cout << "x=" << _x << ", y=" << _y << ", r=" << _radius << endl;
cout << "^" << _shapeName << endl;
}
};
class Rectangle : public Shape{
int _x1, _y1;
int _x2, _y2;
public:
Rectangle(int x1, int y1, int x2, int y2): Shape("rectangle") {
_x1 = x1; _y1 = y1;
_x2 = x2; _y2 = y2;
}
Rectangle(): Shape("rectangle") {
_x1 = 0; _y1 = 0;
_x2 = 1; _y2 = 1;
}
Shape* clone() const override {
return (Shape*)new Rectangle(_x1, _y1, _x2, _y2);
};
void print() {
cout << "x1=" << _x1 << ", y1=" << _y1 << ", x2=" << _x2 << ", y2=" << _y2 << endl;
cout << "^" << _shapeName << endl;
}
};
int main() {
char choose;
Shape* shape;
bool x;
do {
cout << "Enter shape ('c' or 'r'): ";
cin >> choose;
switch (choose) {
case 'c':
shape = new Circle();
x = false;
break;
case 'r':
shape = new Rectangle();
x = false;
break;
default:
x = true;
}
} while (x);
Shape* shapeCopy = shape->clone();
cout << "original shape: address=" << &shape << ", ";
shape->print();
cout << "shape copy: address=" << &shapeCopy << ", ";
shapeCopy->print();
return 0;
}