📄 factory.cpp
字号:
#include <iostream>
using namespace std;
static const double PI = 3.14;
class Shape
{
public:
virtual double Area() = 0;
};
class Rectangle : public Shape
{
public:
Rectangle( int width, int height );
public:
double Area();
private:
int _width;
int _height;
};
Rectangle::Rectangle( int width, int height )
{
_width = width;
_height = height;
}
double Rectangle::Area()
{
return _width*_height;
}
class Circle : public Shape
{
public:
Circle( int radius );
public:
double Area();
private:
int _radius;
};
class Creator
{
virtual Shape* CreateShape() = 0;
};
class RectCreator
{
Shape* CreateShape()
{
Rectangle* rect = new Rectangle( 10, 20 );
return rect;
}
};
class CircleCreator
{
Shape* CreateShape()
{
Circle* rect = new Circle( 10 );
return rect;
}
};
void Compute( Creator* pCreator )
{
Shape* pShape = pCreator->CreateShape();
pShape->Area();
delete pShape;
pShape = NULL;
}
int main()
{
CircleShape cs;
RectShape rs;
Compute( &cs );
Compute( &rs );
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -