#include <windows.h>
#include <iostream>
using namespace std;
// Базовый класс Фигура
class Figure
{
protected:
HDC hdc;
public:
Figure(HDC dc = NULL) : hdc(dc)
{
cout << "Constructor Figure" << endl;
}
virtual ~Figure()
{
cout << "Destructor Figure" << endl;
}
};
// Класс Прямоугольник
class RectangleFigure : virtual public Figure
{
protected:
int x1, y1, x2, y2;
public:
RectangleFigure(HDC dc, int a, int b, int c, int d)
: Figure(dc), x1(a), y1(b), x2(c), y2(d)
{
cout << "Constructor RectangleFigure" << endl;
}
void DrawRectangle()
{
::Rectangle(hdc, x1, y1, x2, y2);
}
~RectangleFigure()
{
cout << "Destructor RectangleFigure" << endl;
}
};
// Класс Окружность
class Circle : virtual public Figure
{
protected:
int radius;
public:
Circle(HDC dc, int r)
: Figure(dc), radius(r)
{
cout << "Constructor Circle" << endl;
}
void DrawCircle(int cx, int cy)
{
::Ellipse(
hdc,
cx - radius, cy - radius,
cx + radius, cy + radius
);
}
~Circle()
{
cout << "Destructor Circle" << endl;
}
};
// Составная фигура
class CompositeFigure :
public RectangleFigure,
public Circle
{
private:
int circleX;
int circleY;
int circleSpacing;
public:
CompositeFigure(
HDC dc,
int x1, int y1, int x2, int y2,
int cx, int cy, int r, int spacing
)
: Figure(dc),
RectangleFigure(dc, x1, y1, x2, y2),
Circle(dc, r),
circleX(cx),
circleY(cy),
circleSpacing(spacing)
{
cout << "Constructor CompositeFigure" << endl;
}
void Draw()
{
// Рисуем прямоугольник
DrawRectangle();
// Рисуем три соприкасающиеся окружности
int step = 2 * radius + circleSpacing;
DrawCircle(circleX, circleY);
DrawCircle(circleX + step, circleY);
DrawCircle(circleX + 2 * step, circleY);
}
~CompositeFigure()
{
cout << "Destructor CompositeFigure" << endl;
}
};
int main()
{
// Получаем существующее консольное окно
HWND hwnd = GetConsoleWindow();
if (hwnd == NULL)
{
cout << "Окно не найдено!" << endl;
return 1;
}
// Получаем HDC существующего окна
HDC hdc = GetDC(hwnd);
if (hdc == NULL)
{
cout << "Ошибка получения HDC!" << endl;
return 1;
}
cout << "=== Создание объекта ===" << endl;
{
CompositeFigure* compFig =
new CompositeFigure(
hdc,
50, 50, 250, 150,
100, 250, 30, 0
);
// Рисование через HDC
compFig->Draw();
cout << "=== Удаление объекта ===" << endl;
delete compFig;
}
cout << "Нажмите Enter для завершения..." << endl;
cin.get();
// Освобождаем HDC
ReleaseDC(hwnd, hdc);
return 0;
}