-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactory Method.cpp
More file actions
57 lines (48 loc) · 1.05 KB
/
Factory Method.cpp
File metadata and controls
57 lines (48 loc) · 1.05 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
class Shape {
public:
virtual void draw() = 0;
virtual ~Shape() {}
};
class Circle : public Shape {
public:
void draw() override {
std::cout << "Drawing Circle\n";
}
};
class Square : public Shape {
public:
void draw() override {
std::cout << "Drawing Square\n";
}
};
class ShapeFactory {
public:
virtual Shape* createShape() = 0; // Factory method
virtual ~ShapeFactory() {}
};
class CircleFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Circle();
}
};
class SquareFactory : public ShapeFactory {
public:
Shape* createShape() override {
return new Square();
}
};
int main() {
ShapeFactory* factory;
factory = new CircleFactory();
Shape* shape1 = factory->createShape();
shape1->draw(); // Output: Drawing Circle
delete shape1;
delete factory;
factory = new SquareFactory();
Shape* shape2 = factory->createShape();
shape2->draw(); // Output: Drawing Square
delete shape2;
delete factory;
return 0;
}