Çʵå Å×½ºÆ®
1. Shape Ŭ·¡½ºÀÇ ÀÚ½Ä Å¬·¡½º·Î »ï°¢Çü, Ÿ¿ø, ¼± Ŭ·¡½º¸¦ ¸¸µé¾îº¸ÀÚ. ¶ÇÇÑ ´ÙÇü¼ºÀ» »ç¿ëÇØ¼ ÀÌ Å¬·¡½ºµéÀÇ °´Ã¼¸¦ ÇϳªÀÇ ¹è¿¿¡ ´ã¾Æ¼ »ç¿ëÇÏ´Â ¿¹Á¦¸¦ ¸¸µé¾îº¸ÀÚ.
Á¤´ä->
[¼Ò½º 23-7]¿¡ »ï°¢Çü Ŭ·¡½º¸¦ Ãß°¡Çغ¸¾Ò´Ù. Ÿ¿ø°ú ¼± Ŭ·¡½º´Â ¿©·¯ºÐÀÌ Á÷Á¢ ÇØº¸±â ¹Ù¶õ´Ù( Shape, Rectangle, Circle Ŭ·¡½º¿Í °ü·ÃµÈ ¼Ò½º ÄÚµå´Â »ý·«Çß´Ù).
// »ï°¢Çü
class Triangle : public Shape
{
public:
Triangle(int
x, int y, int x1, int y1, int x2, int y2);
virtual
void Draw() const;
protected:
int
_x1, _x2, _y1, _y2;
};
Triangle::Triangle(int x, int y, int x1, int y1,
int x2, int y2)
: Shape( x, y)
{
_x1
= x1;
_y1
= y1;
_x2
= x2;
_y2
= y2;
}
void Triangle::Draw() const
{
cout
<< "[Triangle] Position = ( " << _x << ",
" << _y << ") "
"vertex1
= ( " << _x1 << ", " << _y1<< ")
"
"vertex2
= ( " << _x2 << ", " << _y2<<
")\n";
}
int main()
{
//
µµÇüµéÀ» ´ãÀ» ¹è¿À» ÁغñÇÑ´Ù
Shape*
shapes[3] = {NULL};
//
°¢ ŸÀÔÀÇ °´Ã¼¸¦ »ý¼ºÇؼ ¹è¿¿¡ º¸°üÇÑ´Ù.
shapes[0]
= new Circle( 100, 100, 50);
shapes[1]
= new Rectangle( 300, 300, 100, 100);
shapes[2]
= new Triangle( 200, 100, 50, 50, 150, 150);
//
¹è¿ÀÇ º¸°üµÈ ¸ðµç °´Ã¼¸¦ ±×¸°´Ù.
for
(int i = 0; i < 3; ++i)
shapes[i]->Draw();
//
¹è¿ÀÇ º¸°üµÈ ¸ðµç °´Ã¼¸¦ ¼Ò¸ê½ÃŲ´Ù.
for
(i = 0; i < 3; ++i)
{
delete
shapes[i];
shapes[i]
= NULL;
}
return
0;
}
2. Shape Ŭ·¡½º¿¡¼ _x, _y ·Î ÁÂÇ¥¸¦ º¸°üÇÏ´Â ´ë½Å¿¡ Point Ŭ·¡½º¸¦ »ç¿ëÇÏ°Ô °íÃĺ¸ÀÚ. Point °´Ã¼¸¦ ÁÖ°í ¹ÞÀ» ¼ö ÀÖ´Â Á¢±ÙÀÚµµ Ãß°¡ÇØ¾ß ÇÑ´Ù.
Á¤´ä->
class Shape
{
public:
void
Move(double x, double y);
void
Move(const Point& pos);
virtual
void Draw() const = 0;
Shape();
Shape(double
x, double y);
protected:
Point _pos;
};
Shape::Shape()
{
}
Shape::Shape(double
x, double y)
: _pos(x, y)
{
}
void
Shape::Move(double x, double y)
{
_pos.SetX(x);
_pos.SetY(y);
}
void
Shape::Move(const Point& pos)
{
Move(
pos.GetX(), pos.GetY() );
}
Shape Ŭ·¡½º¸¦ ´ÙÀ½°ú °°ÀÌ ¼öÁ¤Çغ¸¾Ò´Ù. Point Ŭ·¡½º¸¦ »ç¿ëÇϹǷΠPoint.h ÆÄÀÏÀ» #include ÇØÁÙ Çʿ䰡 ÀÖ´Ù. ¶ÇÇÑ ÁÂÇ¥ ŸÀÔÀ¸·Î double À» »ç¿ëÇϹǷΠPoint Ŭ·¡½ºÀÇ ¾ÕºÎºÐÀ» ´ÙÀ½°ú °°ÀÌ ¼öÁ¤ÇØ ÁÖ¾î¾ß ÇÑ´Ù.
// Point Ŭ·¡½º¸¦ Á¤ÀÇÇÑ´Ù.
class Point
{
public:
enum
{ MIN_X = 0, MAX_X = 100, MIN_Y = 0, MAX_Y = 100 };
typedef
double COOR_T; // ÁÂÇ¥ÀÇ Å¸ÀÔ
3. DocWriter¿Í HTMLWriter Ŭ·¡½º°¡ ¿Ïº®ÇÑ ´ÙÇü¼ºÀ» °®°Ô µÇ¾ú´Ù. ºÎǰÀÇ ±³Ã¼ Â÷¿ø¿¡¼ DocWriter¿Í HTMLWriter Ŭ·¡½º°¡ °®´Â ´ÙÇü¼ºÀ» ¼³¸íÇØº¸ÀÚ.
Á¤´ä->
DocWriter¿Í HTMLWriter´Â µ¿ÀÏÇÑ ÀÎÅÍÆäÀ̽º¸¦ °¡Áö°í Àֱ⠶§¹®¿¡, DocWriter³ª HTMLWriter¸¦ »ç¿ëÇÏ´Â ÄÚµå´Â µ¿ÀÏÇØÁø´Ù. ±×·¡¼ DocWriter¸¦ »ç¿ëÇÏ´Â ÄÚµå´Â ¼öÁ¤¾øÀÌ HTMLWriter¸¦ »ç¿ëÇÒ ¼ö ÀÖ´Ù.