#include #include using namespace std; class Shape { public: virtual void draw() = 0; }; class Circle :public Shape { public: void draw() { cout << "Circle draw" << endl; } }; class Square :public Shape { public: void draw() { cout << "Square draw" << endl; } }; class Rectangle :public Shape { public: void draw() { cout << "Rectangle draw" << endl; } }; class ShapeFactory { public: Shape* getShape(string ShapeType) { if (ShapeType == "CIRCLE") { return new Circle(); } else if (ShapeType == "RECTANGLE") { return new Rectangle(); } else if (ShapeType == "SQUARE") { return new Square(); } return nullptr; } }; void main() { ShapeFactory obj; Shape* shape1 = obj.getShape("CIRCLE"); shape1->draw(); Shape* shape2 = obj.getShape("SQUARE"); shape2->draw(); Shape* shape3 = obj.getShape("RECTANGLE"); shape3->draw(); } Note: Code is given Explain as soon as possible
Explain c++ Factory design code by just applying double line comments
#include<iostream>
#include<string>
using namespace std;
class Shape
{
public:
virtual void draw() = 0;
};
class Circle :public Shape
{
public:
void draw()
{
cout << "Circle draw" << endl;
}
};
class Square :public Shape
{
public:
void draw()
{
cout << "Square draw" << endl;
}
};
class Rectangle :public Shape
{
public:
void draw()
{
cout << "Rectangle draw" << endl;
}
};
class ShapeFactory
{
public:
Shape* getShape(string ShapeType)
{
if (ShapeType == "CIRCLE")
{
return new Circle();
}
else if (ShapeType == "RECTANGLE")
{
return new Rectangle();
}
else if (ShapeType == "SQUARE")
{
return new Square();
}
return nullptr;
}
};
void main()
{
ShapeFactory obj;
Shape* shape1 = obj.getShape("CIRCLE");
shape1->draw();
Shape* shape2 = obj.getShape("SQUARE");
shape2->draw();
Shape* shape3 = obj.getShape("RECTANGLE");
shape3->draw();
}
Note:
Code is given
Explain as soon as possible
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images