/
lyapdy
/
skillbox_controllers
Обзор
Документация
Войти
/
lyapdy
/
skillbox_controllers
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
vector3d_project1/src/Vector3D.cpp
190 строк
5 KB
danillyapunov
task9 commit
13 дек 2025, 16:42
13 дек 2025, 16:42
916f5d0
Код
Авторство
О чём код?
#include "Vector3D.h" #include <cmath> #include <algorithm> // Инициализация статического члена int Vector3D::objectCount = 0; // Конструктор по умолчанию Vector3D::Vector3D() : coords(std::make_unique<double[]>(3)) { coords[0] = 0.0; coords[1] = 0.0; coords[2] = 0.0; objectCount++; std::cout << "Default constructor. Objects count: " << objectCount << std::endl; } // Конструктор с параметрами Vector3D::Vector3D(double x, double y, double z) : coords(std::make_unique<double[]>(3)) { coords[0] = x; coords[1] = y; coords[2] = z; objectCount++; std::cout << "Parameterized constructor. Objects count: " << objectCount << std::endl; } // Конструктор копирования Vector3D::Vector3D(const Vector3D& other) : coords(std::make_unique<double[]>(3)) { copyFrom(other); objectCount++; std::cout << "Copy constructor. Objects count: " << objectCount << std::endl; } // Конструктор перемещения Vector3D::Vector3D(Vector3D&& other) noexcept : coords(std::move(other.coords)) { // After moving, other.coords becomes nullptr objectCount++; std::cout << "Move constructor. Objects count: " << objectCount << std::endl; } // Оператор присваивания копированием Vector3D& Vector3D::operator=(const Vector3D& other) { if (this != &other) { copyFrom(other); } std::cout << "Copy assignment operator" << std::endl; return *this; } // Оператор присваивания перемещением Vector3D& Vector3D::operator=(Vector3D&& other) noexcept { if (this != &other) { // Unique pointer will automatically release old resources coords = std::move(other.coords); // other.coords is now nullptr } std::cout << "Move assignment operator" << std::endl; return *this; } // Деструктор Vector3D::~Vector3D() { objectCount--; std::cout << "Destructor called. Objects count: " << objectCount << std::endl; } // Приватный метод для копирования void Vector3D::copyFrom(const Vector3D& other) { if (other.coords) { if (!coords) { coords = std::make_unique<double[]>(3); } coords[0] = other.coords[0]; coords[1] = other.coords[1]; coords[2] = other.coords[2]; } } // Методы доступа double Vector3D::getX() const { return coords ? coords[0] : 0.0; } double Vector3D::getY() const { return coords ? coords[1] : 0.0; } double Vector3D::getZ() const { return coords ? coords[2] : 0.0; } void Vector3D::setX(double x) { if (coords) coords[0] = x; } void Vector3D::setY(double y) { if (coords) coords[1] = y; } void Vector3D::setZ(double z) { if (coords) coords[2] = z; } // Операции с вектором double Vector3D::length() const { if (!coords) return 0.0; return std::sqrt(coords[0]*coords[0] + coords[1]*coords[1] + coords[2]*coords[2]); } void Vector3D::normalize() { double len = length(); if (len > 0.0 && coords) { coords[0] /= len; coords[1] /= len; coords[2] /= len; } } Vector3D Vector3D::add(const Vector3D& other) const { return Vector3D(getX() + other.getX(), getY() + other.getY(), getZ() + other.getZ()); } Vector3D Vector3D::subtract(const Vector3D& other) const { return Vector3D(getX() - other.getX(), getY() - other.getY(), getZ() - other.getZ()); } double Vector3D::dotProduct(const Vector3D& other) const { return getX() * other.getX() + getY() * other.getY() + getZ() * other.getZ(); } // Вывод void Vector3D::print() const { std::cout << "Vector3D(" << getX() << ", " << getY() << ", " << getZ() << ")" << std::endl; } int Vector3D::getObjectCount() { return objectCount; } // Операторы Vector3D Vector3D::operator+(const Vector3D& other) const { return add(other); } Vector3D Vector3D::operator-(const Vector3D& other) const { return subtract(other); } Vector3D& Vector3D::operator+=(const Vector3D& other) { if (coords) { coords[0] += other.getX(); coords[1] += other.getY(); coords[2] += other.getZ(); } return *this; } Vector3D& Vector3D::operator-=(const Vector3D& other) { if (coords) { coords[0] -= other.getX(); coords[1] -= other.getY(); coords[2] -= other.getZ(); } return *this; } bool Vector3D::operator==(const Vector3D& other) const { return getX() == other.getX() && getY() == other.getY() && getZ() == other.getZ(); } // Функции для работы с умными указателями void normalizeVector(std::shared_ptr<Vector3D> vec) { if (vec) { std::cout << "Normalizing vector (shared_ptr): "; vec->print(); vec->normalize(); std::cout << "After normalization: "; vec->print(); } } void printVectorInfo(std::shared_ptr<Vector3D> vec) { if (vec) { std::cout << "Vector info (shared_ptr): "; vec->print(); std::cout << "Length: " << vec->length() << std::endl; std::cout << "Use count: " << vec.use_count() << std::endl; } } std::shared_ptr<Vector3D> createSharedVector(double x, double y, double z) { return std::make_shared<Vector3D>(x, y, z); }