#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 circleX, int circleY)
{
::Ellipse(
hdc,
circleX - radius,
circleY - radius,
circleX + radius,
circleY + 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()
{
// Создаём окно Windows
HWND hwnd = CreateWindowExA(
0,
"STATIC",
"Lab 4 Variant 1.6",
WS_OVERLAPPEDWINDOW,
100, 100,
800, 600,
NULL, NULL,
GetModuleHandle(NULL),
NULL
);
if (hwnd == NULL)
{
cout << "Ошибка создания окна!" << endl;
return 1;
}
ShowWindow(hwnd, SW_SHOW);
UpdateWindow(hwnd);
// Получаем контекст устройства
HDC hdc = GetDC(hwnd);
if (hdc == NULL)
{
cout << "Ошибка получения HDC!" << endl;
DestroyWindow(hwnd);
return 1;
}
cout << "=== Создание объекта ===" << endl;
{
CompositeFigure* compFig =
new CompositeFigure(
hdc,
100, 100, 400, 200,
150, 300, 50, 0
);
// Рисуем фигуры
compFig->Draw();
cout << "=== Удаление объекта ===" << endl;
delete compFig;
}
cout << "Нажмите Enter для завершения..." << endl;
cin.get();
// Освобождаем ресурсы
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
return 0;
}