#include <windows.h>
#include <iostream>
using namespace std;
// Базовый класс фигура
class Figure {
protected:
HDC hdc;
public:
Figure(HDC dc) : hdc(dc) {
cout << "Figure constructor called\n";
}
virtual ~Figure() {
cout << "Figure destructor called\n";
}
virtual void draw() = 0;
};
// Класс прямоугольник
class Rectangle : public virtual Figure {
private:
int x, y, width, height;
public:
Rectangle(HDC dc, int x, int y, int w, int h)
: Figure(dc), x(x), y(y), width(w), height(h) {
cout << "Rectangle constructor called\n";
}
~Rectangle() {
cout << "Rectangle destructor called\n";
}
void draw() override {
Rectangle(hdc, x, y, x + width, y + height);
}
};
// Класс окружность
class Circle : public virtual Figure {
private:
int x, y, radius;
public:
Circle(HDC dc, int x, int y, int r)
: Figure(dc), x(x), y(y), radius(r) {
cout << "Circle constructor called\n";
}
~Circle() {
cout << "Circle destructor called\n";
}
void draw() override {
Ellipse(hdc, x - radius, y - radius, x + radius, y + radius);
}
};
// Составной класс — прямоугольник с соприкасающимися окружностями
class CompositeFigure : public Rectangle, public Circle {
private:
int circleSpacing;
public:
CompositeFigure(HDC dc, int rectX, int rectY, int rectW, int rectH,
int circleX, int circleY, int radius, int spacing)
: Figure(dc), Rectangle(dc, rectX, rectY, rectW, rectH),
Circle(dc, circleX, circleY, radius), circleSpacing(spacing) {
cout << "CompositeFigure constructor called\n";
}
~CompositeFigure() {
cout << "CompositeFigure destructor called\n";
}
void draw() override {
// Рисуем прямоугольник
Rectangle::draw();
// Рисуем соприкасающиеся окружности
int currentX = circleX;
for (int i = 0; i < 3; ++i) {
Ellipse(hdc, currentX - radius, circleY - radius,
currentX + radius, circleY + radius);
currentX += 2 * radius + circleSpacing;
}
}
};
// Главная функция
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
// Создаём окно
HWND hwnd = CreateWindow("STATIC", "Lab 4 Variant 1.6",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT,
800, 600, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Получаем контекст устройства
HDC hdc = GetDC(hwnd);
cout << "=== Создание объекта (виртуальное наследование) ===\n";
CompositeFigure* compFig = new CompositeFigure(hdc, 100, 100, 400, 200,
150, 300, 50, 10);
compFig->draw();
cout << "\n=== Удаление объекта ===\n";
delete compFig;
ReleaseDC(hwnd, hdc);
return 0;
}