Загрузка данных
#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() override {
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() override {
cout << "Circle destructor called\n";
}
int getX() const {
return x;
}
int getY() const {
return y;
}
int getRadius() const {
return radius;
}
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() override {
cout << "CompositeFigure destructor called\n";
}
void draw() override {
// Рисуем прямоугольник
Rectangle::draw();
// Получаем параметры окружностей
int currentX = Circle::getX();
int circleY = Circle::getY();
int radius = Circle::getRadius();
// Рисуем три окружности
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 = CreateWindowA(
"STATIC",
"Lab 4 Variant 1.6",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT,
800, 600,
NULL, NULL,
hInstance, NULL
);
if (hwnd == NULL) {
cout << "Window creation failed!\n";
return 1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Получаем контекст устройства
HDC hdc = GetDC(hwnd);
if (hdc == NULL) {
cout << "Failed to get device context!\n";
DestroyWindow(hwnd);
return 1;
}
cout << "=== Creating object (virtual inheritance) ===\n";
CompositeFigure* compFig = new CompositeFigure(
hdc,
100, 100, 400, 200,
150, 300, 50, 10
);
compFig->draw();
cout << "\n=== Deleting object ===\n";
delete compFig;
// Освобождаем ресурсы
ReleaseDC(hwnd, hdc);
DestroyWindow(hwnd);
system("pause");
return 0;
}