#include <windows.h>
#include <iostream>
using namespace std;
// Базовый класс Фигура
class Figure
{
protected:
HDC hdc;
int x, y;
public:
Figure(HDC dc = NULL, int x0 = 0, int y0 = 0)
: hdc(dc), x(x0), y(y0)
{
cout << "Constructor Figure" << endl;
}
virtual ~Figure()
{
cout << "Destructor Figure" << endl;
}
};
// Класс Прямоугольник
class RectFigure : virtual public Figure
{
public:
RectFigure(HDC dc, int x0, int y0)
: Figure(dc, x0, y0)
{
cout << "Constructor RectFigure" << endl;
}
void DrawRectangle()
{
::Rectangle(hdc, x, y, x + 100, y + 60);
}
~RectFigure()
{
cout << "Destructor RectFigure" << endl;
}
};
// Класс Окружность
class Circle : virtual public Figure
{
protected:
int radius;
public:
Circle(HDC dc, int x0, int y0, int r)
: Figure(dc, x0, y0), 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 TouchingCircles : public RectFigure, public Circle
{
public:
TouchingCircles(HDC dc, int x0, int y0)
: Figure(dc, x0, y0),
RectFigure(dc, x0, y0),
Circle(dc, x0, y0, 30)
{
cout << "Constructor TouchingCircles" << endl;
}
void Draw()
{
// Рисование прямоугольника через HDC
DrawRectangle();
// Рисование трех соприкасающихся окружностей через HDC
DrawCircle(x + 30, y + 100);
DrawCircle(x + 90, y + 100);
DrawCircle(x + 150, y + 100);
}
~TouchingCircles()
{
cout << "Destructor TouchingCircles" << endl;
}
};
int main()
{
// Создание окна Windows
HWND hwnd = CreateWindowExA(
0,
"STATIC",
"Laboratory 4 - Variant 1.6",
WS_OVERLAPPEDWINDOW | WS_VISIBLE,
100, 100, 500, 400,
NULL, NULL, GetModuleHandle(NULL), NULL
);
if (hwnd == NULL)
{
cout << "Window creation error" << endl;
return 1;
}
// Получение контекста устройства
HDC hdc = GetDC(hwnd);
if (hdc == NULL)
{
DestroyWindow(hwnd);
cout << "HDC error" << endl;
return 1;
}
{
// Создание объекта составной фигуры
TouchingCircles figure(hdc, 50, 50);
// Отрисовка фигур
figure.Draw();
cout << "Press Enter to exit..." << endl;
cin.get();
}
// Освобождение контекста устройства
ReleaseDC(hwnd, hdc);
// Закрытие окна
DestroyWindow(hwnd);
return 0;
}