TOC
TOC
TOC
Example 10-1
Rectangle
TOP
Rectangle.h
#pragma once
class Rectangle
{
public:
Rectangle(double, double, double, double);
virtual void drawLine(double, double, double, double)=0;
void draw();
public:
~Rectangle(void);
private:
double _x1, _x2, _y1, _y2;
};
Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle(double x1, double y1, double x2, double y2)
{
_x1= x1; _y1= y1; _x2= x2; _y2= y2;
}
Rectangle::~Rectangle(void)
{
}
void Rectangle::draw()
{
drawLine( _x1, _y1, _x2, _y1);
drawLine( _x2, _y1, _x2, _y2);
drawLine( _x2, _y2, _x1, _y2);
drawLine( _x1, _y2, _x1, _y1);
}
V1Rectangle
TOP
V1Rectangle.h
#pragma once
#include "Rectangle.h"
class V1Rectangle : public Rectangle
{
public:
V1Rectangle(double, double, double, double);
void drawLine( double, double, double, double);
public:
~V1Rectangle(void);
};
V1Rectangle.cpp
#include "V1Rectangle.h"
#include "DP1.h"
V1Rectangle::V1Rectangle(double x1, double y1, double x2, double y2) :
Rectangle(x1, y1, x2, y2)
{
}
V1Rectangle::~V1Rectangle(void)
{
}
void V1Rectangle::drawLine( double x1, double y1, double x2, double y2)
{
DP1::draw_a_line( x1, y1, x2, y2);
}
V2Rectangle
TOP
V2Rectangle.h
#pragma once
#include "Rectangle.h"
class V2Rectangle : public Rectangle
{
public:
V2Rectangle(double, double, double, double);
void drawLine( double, double, double, double);
public:
~V2Rectangle(void);
};
V2Rectangle.cpp
#include "V2Rectangle.h"
#include "DP2.h"
V2Rectangle::V2Rectangle(double x1, double y1, double x2, double y2) :
Rectangle(x1, y1, x2, y2)
{
}
V2Rectangle::~V2Rectangle(void)
{
}
void V2Rectangle::drawLine( double x1, double y1, double x2, double y2)
{
DP2::drawline( x1, y1, x2, y2);
}
DP1
TOP
DP1.h
#pragma once
class DP1
{
public:
DP1(void);
static void draw_a_line( double, double, double, double);
static void draw_a_circle( double, double, double);
public:
~DP1(void);
};
DP1.cpp
#include "DP1.h"
DP1::DP1(void)
{
}
DP1::~DP1(void)
{
}
void DP1::draw_a_line( double x1, double y1, double x2, double y2)
{
// draw the line
}
void DP1::draw_a_circle( double x, double y, double r)
{
// draw the circle
}
DP2
TOP
DP2.h
#pragma once
class DP2
{
public:
DP2(void);
static void drawline( double, double, double, double);
static void drawcircle( double, double, double);
public:
~DP2(void);
};
DP2.cpp
#include "DP2.h"
DP2::DP2(void)
{
}
DP2::~DP2(void)
{
}
void DP2::drawline( double x1, double x2, double y1, double y2)
{
// draw the line
}
void DP2::drawcircle( double x, double y, double r)
{
// draw the circle
}
Example 10-2
Shape
TOP
Shape.h
#pragma once
class Shape
{
public:
Shape(void);
virtual void draw()= 0;
public:
~Shape(void);
};
Shape.cpp
#include "Shape.h"
Shape::Shape(void)
{
}
Shape::~Shape(void)
{
}
Circle
TOP
Circle.h
#pragma once
#include "Shape.h"
class Circle :
public Shape
{
public:
Circle(double, double, double);
virtual void draw();
virtual void drawCircle(double, double, double)=0;
public:
~Circle(void);
protected:
double _x, _y, _r;
};
Circle.cpp
#include "Circle.h"
Circle::Circle(double x, double y, double r)
{
_x= x;
_y= y;
_r= r;
}
Circle::~Circle(void)
{
}
void Circle::draw ()
{
drawCircle( _x, _y, _r);
}
Rectangle
TOP
Rectangle.h
#pragma once
#include "Shape.h"
class Rectangle : public Shape
{
public:
Rectangle(double, double, double, double);
virtual void drawLine(double, double, double, double)=0;
void draw();
public:
~Rectangle(void);
private:
double _x1, _x2, _y1, _y2;
};
Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle(double x1, double y1, double x2, double y2)
{
_x1= x1; _y1= y1; _x2= x2; _y2= y2;
}
Rectangle::~Rectangle(void)
{
}
void Rectangle::draw()
{
drawLine( _x1, _y1, _x2, _y1);
drawLine( _x2, _y1, _x2, _y2);
drawLine( _x2, _y2, _x1, _y2);
drawLine( _x1, _y2, _x1, _y1);
}
V1Circle
TOP
V1Circle.h
#pragma once
#include "Circle.h"
class V1Circle : public Circle
{
public:
V1Circle(double, double, double);
void drawCircle( double, double, double);
public:
~V1Circle(void);
};
V1Circle.cpp
#include "V1Circle.h"
#include "DP1.h"
V1Circle::V1Circle(double x, double y, double r) : Circle( x, y, r)
{
}
V1Circle::~V1Circle(void)
{
}
void V1Circle::drawCircle ( double x, double y, double r)
{
DP1::draw_a_circle( x, y, r);
}
V1Rectangle
TOP
V1Rectangle.h
#pragma once
#include "Rectangle.h"
class V1Rectangle : public Rectangle
{
public:
V1Rectangle(double, double, double, double);
void drawLine( double, double, double, double);
public:
~V1Rectangle(void);
};
V1Rectangle.cpp
#include "V1Rectangle.h"
#include "DP1.h"
V1Rectangle::V1Rectangle(double x1, double y1, double x2, double y2) :
Rectangle(x1, y1, x2, y2)
{
}
V1Rectangle::~V1Rectangle(void)
{
}
void V1Rectangle::drawLine( double x1, double y1, double x2, double y2)
{
DP1::draw_a_line( x1, y1, x2, y2);
}
V2Circle
TOP
V2Circle.h
#pragma once
#include "Circle.h"
class V2Circle : public Circle
{
public:
V2Circle(double, double, double);
void drawCircle( double, double, double);
public:
~V2Circle(void);
};
V2Circle.cpp
#include "V2Circle.h"
#include "DP2.h"
V2Circle::V2Circle(double x, double y, double r) : Circle( x, y, r)
{
}
V2Circle::~V2Circle(void)
{
}
void V2Circle::drawCircle ( double x, double y, double r)
{
DP2::drawcircle( x, y, r);
}
V2Rectangle
TOP
V2Rectangle.h
#pragma once
#include "Rectangle.h"
class V2Rectangle : public Rectangle
{
public:
V2Rectangle(double, double, double, double);
void drawLine( double, double, double, double);
public:
~V2Rectangle(void);
};
V2Rectangle.cpp
#include "V2Rectangle.h"
#include "DP2.h"
V2Rectangle::V2Rectangle(double x1, double y1, double x2, double y2) :
Rectangle(x1, y1, x2, y2)
{
}
V2Rectangle::~V2Rectangle(void)
{
}
void V2Rectangle::drawLine( double x1, double y1, double x2, double y2)
{
DP2::drawline( x1, y1, x2, y2);
}
DP1
TOP
DP1.h
#pragma once
class DP1
{
public:
DP1(void);
static void draw_a_line( double, double, double, double);
static void draw_a_circle( double, double, double);
public:
~DP1(void);
};
DP1.cpp
#include "DP1.h"
DP1::DP1(void)
{
}
DP1::~DP1(void)
{
}
void DP1::draw_a_line( double x1, double y1, double x2, double y2)
{
// draw the line
}
void DP1::draw_a_circle( double x, double y, double r)
{
// draw the circle
}
DP2
TOP
DP2.h
#pragma once
class DP2
{
public:
DP2(void);
static void drawline( double, double, double, double);
static void drawcircle( double, double, double);
public:
~DP2(void);
};
DP2.cpp
#include "DP2.h"
DP2::DP2(void)
{
}
DP2::~DP2(void)
{
}
void DP2::drawline( double x1, double x2, double y1, double y2)
{
// draw the line
}
void DP2::drawcircle( double x, double y, double r)
{
// draw the circle
}
Example 10-3
Client
TOP
Client.h
#pragma once
class Client
{
public:
Client(void);
public:
~Client(void);
};
Client.cpp
#include "Client.h"
#include "Shape.h"
#include "Factory.h"
Client::Client(void)
{
}
Client::~Client(void)
{
}
void static main ()
{
Shape **myShapes;
Factory myFactory;
// get Rectangles from some other source
myShapes= myFactory.getShapes();
for (int i= 0; i < sizeof(myShapes)/ sizeof(myShapes[0]); i++)
myShapes[i]->draw();
}
Factory
TOP
Factory.h
#pragma once
#include "Shape.h"
#include "Circle.h"
#include "Rectangle.h"
#include "Drawing.h"
#include "V1Drawing.h"
#include "V2Drawing.h"
class Factory
{
public:
Factory(void);
~Factory(void);
Shape** getShapes();
};
Factory.cpp
#include "Factory.h"
Factory::Factory(void)
{
}
Factory::~Factory(void)
{
}
Shape** Factory::getShapes ()
{
Shape **myShapes;
// Figure out the shapes needed.
// create them and put them in the array myShapes
return myShapes;
}
Shape
TOP
Shape.h
#pragma once
#include "Drawing.h"
class Shape
{
public:
Shape(Drawing *aDrawing);
virtual void draw()= 0;
protected:
Drawing *myDrawing;
void drawLine( double, double, double, double);
void drawCircle( double, double, double);
public:
~Shape(void);
};
Shape.cpp
#include "Shape.h"
Shape::Shape(Drawing *aDrawing)
{
myDrawing= aDrawing;
}
Shape::~Shape(void)
{
}
void Shape::drawLine( double x1,double y1, double x2,double y2)
{
myDrawing->drawLine(x1,y1,x2,y2);
}
void Shape::drawCircle( double x,double y,double r)
{
myDrawing->drawCircle(x,y,r);
}
Circle
TOP
Circle.h
#pragma once
#include "Shape.h"
class Circle : public Shape
{
public:
Circle(Drawing*, double, double, double);
virtual void draw();
virtual void drawCircle(double, double, double)=0;
public:
~Circle(void);
protected:
double _x, _y, _r;
};
Circle.cpp
#include "Circle.h"
#include "Factory.h"
Circle::Circle(Drawing *aDrawing, double x, double y, double r) : Shape (aDrawing)
{
_x= x;
_y= y;
_r= r;
}
Circle::~Circle(void)
{
}
void Circle::draw ()
{
drawCircle( _x, _y, _r);
}
Rectangle
TOP
Rectangle.h
#pragma once
#include "Shape.h"
class Rectangle : public Shape
{
public:
Rectangle(Drawing*, double, double, double, double);
virtual void drawLine(double, double, double, double)=0;
void draw();
public:
~Rectangle(void);
private:
double _x1, _x2, _y1, _y2;
};
Rectangle.cpp
#include "Rectangle.h"
Rectangle::Rectangle(Drawing *aDrawing, double x1, double y1, double x2, double y2) :
Shape (aDrawing)
{
_x1= x1; _y1= y1; _x2= x2; _y2= y2;
}
Rectangle::~Rectangle(void)
{
}
void Rectangle::draw()
{
drawLine( _x1, _y1, _x2, _y1);
drawLine( _x2, _y1, _x2, _y2);
drawLine( _x2, _y2, _x1, _y2);
drawLine( _x1, _y2, _x1, _y1);
}
Drawing
TOP
Drawing.h
#pragma once
class Drawing
{
public:
Drawing(void);
public:
~Drawing(void);
virtual void drawLine( double, double, double, double)=0;
virtual void drawCircle( double, double, double)=0;
};
Drawing.cpp
#include "Drawing.h"
Drawing::Drawing(void)
{
}
Drawing::~Drawing(void)
{
}
V1Drawing
TOP
V1Drawing.h
#pragma once
#include "drawing.h"
#include "DP1.h"
class V1Drawing : public Drawing
{
public:
V1Drawing(void);
public:
~V1Drawing(void);
void drawLine( double, double, double, double);
void drawCircle( double, double, double);
private:
DP1 myDP1;
};
V1Drawing.cpp
#include "V1Drawing.h"
V1Drawing::V1Drawing(void)
{
}
V1Drawing::~V1Drawing(void)
{
}
void V1Drawing::drawLine ( double x1,double y1, double x2,double y2)
{
DP1::draw_a_line(x1,y1,x2,y2);
}
void V1Drawing::drawCircle (double x,double y,double r)
{
DP1::draw_a_circle(x,y,r);
}
V2Drawing
TOP
V2Drawing.h
#pragma once
#include "drawing.h"
#include "DP2.h"
class V2Drawing : public Drawing
{
public:
V2Drawing(void);
public:
~V2Drawing(void);
void drawLine( double, double, double, double);
void drawCircle( double, double, double);
private:
DP2 myDP2;
};
V2Drawing.cpp
#include "V2Drawing.h"
V2Drawing::V2Drawing(void)
{
}
V2Drawing::~V2Drawing(void)
{
}
void V2Drawing::drawLine (double x1,double y1, double x2,double y2)
{
// arguments are different in DP2
// and must be rearranged
DP2::drawline(x1,x2,y1,y2);
}
void V2Drawing::drawCircle ( double x, double y,double r)
{
DP2::drawcircle(x,y,r);
}
DP1
TOP
DP1.h
#pragma once
class DP1
{
public:
DP1(void);
static void draw_a_line( double, double, double, double);
static void draw_a_circle( double, double, double);
public:
~DP1(void);
};
DP1.cpp
#include "DP1.h"
DP1::DP1(void)
{
}
DP1::~DP1(void)
{
}
void DP1::draw_a_line( double x1, double y1, double x2, double y2)
{
// draw the line
}
void DP1::draw_a_circle( double x, double y, double r)
{
// draw the circle
}
DP2
TOP
DP2.h
#pragma once
#include "DP2.h"
class DP2
{
public:
DP2(void);
static void drawline( double, double, double, double);
static void drawcircle( double, double, double);
public:
~DP2(void);
};
DP2.cpp
#include "DP2.h"
DP2::DP2(void)
{
}
DP2::~DP2(void)
{
}
void DP2::drawline( double x1, double x2, double y1, double y2)
{
// draw the line
}
void DP2::drawcircle( double x, double y, double r)
{
// draw the circle
}
|