00001
00002
00003
00004 #include <iostream>
00005
00006 using namespace std;
00007
00008
00009 class DrawingAPI {
00010 public:
00011 virtual void drawCircle(double x, double y, double radius) = 0;
00012 virtual ~DrawingAPI() {}
00013 };
00014
00015
00016 class DrawingAPI1 : public DrawingAPI {
00017 public:
00018 void drawCircle(double x, double y, double radius) {
00019 cout << "API1.circle at " << x << ':' << y << ' ' << radius << endl;
00020 }
00021 };
00022
00023
00024 class DrawingAPI2 : public DrawingAPI {
00025 public:
00026 void drawCircle(double x, double y, double radius) {
00027 cout << "API2.circle at " << x << ':' << y << ' ' << radius << endl;
00028 }
00029 };
00030
00031
00032 class Shape {
00033 public:
00034 virtual ~Shape() {}
00035 virtual void draw() = 0;
00036 virtual void resizeByPercentage(double pct) = 0;
00037 };
00038
00039
00040 class CircleShape : public Shape {
00041 public:
00042 CircleShape(double x, double y,double radius, DrawingAPI *drawingAPI) :
00043 m_x(x), m_y(y), m_radius(radius), m_drawingAPI(drawingAPI)
00044 {}
00045 void draw() {
00046 m_drawingAPI->drawCircle(m_x, m_y, m_radius);
00047 }
00048 void resizeByPercentage(double pct) {
00049 m_radius *= pct;
00050 }
00051 private:
00052 double m_x, m_y, m_radius;
00053 DrawingAPI *m_drawingAPI;
00054 };
00055
00056 int main(void) {
00057 CircleShape circle1(1,2,3,new DrawingAPI1());
00058 CircleShape circle2(5,7,11,new DrawingAPI2());
00059 circle1.resizeByPercentage(2.5);
00060 circle2.resizeByPercentage(2.5);
00061 circle1.draw();
00062 circle2.draw();
00063 return 0;
00064 }
00065
00066
00067
00068
00069