Supplement: C++ Code Examples

Example 9-4. C++ Code Fragments: Rectangles Only
void Rectangle::draw () {
   drawLine(_x1,_y1,_x2,_y1);
   drawLine(_x2,_y1,_x2,_y2);
   drawLine(_x2,_y2,_x1,_y2);
   drawLine(_x1,_y2,_x1,_y1);
}

void V1Rectangle::drawLine
   (double x1, double y1,
    double x2, double y2) {
    DP1.draw_a_line(x1,y1,x2,y2);
}

void V2Rectangle::drawLine
   (double x1, double y1,
    double x2, double y2) {
    DP2.drawline(x1,x2,y1,y2);
}

Example 9-5. C++ Code Fragments: Rectangles and Circles without Bridge
class Shape {
  public: void draw ()=0;
}
class Rectangle : Shape {
  public:
    void draw();
  protected:
    void drawLine(
        double x1,y1, x2,y2)=0;
}
void Rectangle::draw () {
  drawLine(_x1,_y1,_x2,_y1);
  drawLine(_x2,_y1,_x2,_y2);
  drawLine(_x2,_y2,_x1,_y2);
  drawLine(_x1,_y2,_x1,_y1);
}
// V1Rectangle and V2Rectangle both derive from
// Rectangle header files not shown
void V1Rectangle::drawLine (
  double x1,y1, x2,y2) {
  DP1.draw_a_line(x1,y1,x2,y2);
}
void V2Rectangle::drawLine (
   double x1,y1, x2,y2) {
   DP2.drawline(x1,x2,y1,y2);
   }
}

class Circle : Shape {
  public:
    void draw() ;
  protected:
    void drawCircle(
      double x, y, z) ;
}
void Circle::draw () {
  drawCircle();

}

// V1Circle and V2Circle both derive from Circle
// header files not shown
void V1Circle::drawCircle (
  DP1.draw_a_circle(x, y, r);
}

void V2Circle::drawCircle (
  DP2.drawcircle(x, y, r);
}

Example 9-6. C++ Code Fragments: The Bridge Implemented
void main (String argv[]) {
  Shape *s1;
  Shape *s2;
  Drawing *dp1, *dp2;

  dp1= new V1Drawing;
  s1=new Rectangle(dp,1,1,2,2);

  dp2= new V2Drawing;
  s2= new Circle(dp,2,2,4);

  s1->draw();
  s2->draw();

  delete s1; delete s2;
  delete dp1; delete dp2;
}

// NOTE: Memory management not tested.
// Includes not shown.

class Shape {
  public: draw()=0;
  private: Drawing *_dp;
}
Shape::Shape (Drawing *dp) {
  _dp= dp;
}
void Shape::drawLine(
  double x1, double y1,
  double x2, double y2)
    _dp->drawLine(x1,y1,x2,y2);
}

Rectangle::Rectangle (Drawing *dp,
  double x1, y1, x2, y2) :
  Shape( dp) {
  _x1= x1; _x2= x2;
  _y1= y1; _y2= y2;
}
void Rectangle::draw () {
  drawLine(_x1,_y1,_x2,_y1);
  drawLine(_x2,_y1,_x2,_y2);
  drawLine(_x2,_y2,_x1,_y2);
  drawLine(_x1,_y2,_x1,_y1);
}
class Circle {
  public: Circle (
      Drawing *dp,
      double x, double y, double r);
};

Circle::Circle (
    Drawing *dp,
    double x, double y,
    double r) : Shape(dp) {
    _x= x;
    _y= y;
    _r= r;
}

Circle::draw () {
    drawCircle( _x, _y, _r);
}

class Drawing {
  public: virtual void drawLine (
      double x1, double y1,
      double x2, double y2)=0;
};

class V1Drawing :
  public Drawing {
    public: void drawLine (
        double x1, double y1,
        double x2, double y2);
      void drawCircle(
        double x, double y, double r);
};

void V1Drawing::drawLine (
  double x1, double y1,
  double x2, double y2) {
  DP1.draw_a_line(x1,y1,x2,y2);
}
void V1Drawing::drawCircle (
   double x1, double y, double r) {
     DP1.draw_a_circle (x,y,r);
}
class V2Drawing : public
   Drawing {
   public:
     void drawLine (
       double x1, double y1,
       double x2, double y2);
     void drawCircle(
       double x, double y, double r);
};
void V2Drawing::drawLine (
  double x1, double y1,
  double x2, double y2) {
    DP2.drawline(x1,x2,y1,y2);
}

void V2Drawing::drawCircle (
  double x, double y, double r) {
    DP2.drawcircle(x, y, r);
}

// We have been given the implementations for
// DP1 and DP2

class DP1 {
  public:
    static void draw_a_line (
      double x1, double y1,
      double x2, double y2);
    static void draw_a_circle (
      double x, double y, double r);
};

class DP2 {
  public:
    static void drawline (
      double x1, double x2,
      double y1, double y2);
    static void drawcircle (
      double x, double y, double r);
};

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset