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);
};
|