Загрузка данных


Elipse.cpp
#include "Ellipse.h"
#include <cmath>
#include <stdexcept>

// Конструктор по умолчанию: единичный эллипс
Ellipse::Ellipse()
    : Function(), a_(1.0), b_(1.0) {
}

// Конструктор с параметрами — обяз. валидация: сначала базовый класс, затем a_, затем b_
Ellipse::Ellipse(double a, double b)
    : Function(), a_(a), b_(b) {
    if (a <= 0.0 || b <= 0.0) {
        throw std::invalid_argument(
            "Ошибка: полуоси эллипса должны быть положительными числами");
    }
}

// Конструктор копирования
Ellipse::Ellipse(const Ellipse& other)
    : Function(other), a_(other.a_), b_(other.b_) {
}

// Деструктор
Ellipse::~Ellipse() {
    a_ = 0.0;
    b_ = 0.0;
}

double Ellipse::getA() const { return a_; }
double Ellipse::getB() const { return b_; }

// Вычисление y = f(x) — верхняя ветвь эллипса
// y = b * sqrt(1 - x^2 / a^2)
double Ellipse::compute(double x) const {
    double expr = 1.0 - (x * x) / (a_ * a_);
    if (expr < 0.0) {
        throw std::invalid_argument(
            "Ошибка: x выходит за границы эллипса (|x| должен быть <= a)");
    }
    return b_ * std::sqrt(expr);
}

// Вывод формулы эллипса
void Ellipse::print(std::ostream& os) const {
    os << "Эллипс: x^2 / " << (a_ * a_)
       << " + y^2 / " << (b_ * b_) << " = 1"
       << " (a = " << a_ << ", b = " << b_ << ")";
}

// operator= для 1 типа
Ellipse& Ellipse::operator=(const Ellipse& other) {
    if (this != &other) {
        a_ = other.a_;
        b_ = other.b_;
    }
    return *this;
}

// operator= для разного типа данных через ссылку
Function& Ellipse::operator=(const Function& other) {
    if (this != &other) {
        const Ellipse& ell = dynamic_cast<const Ellipse&>(other);
        a_ = ell.a_;
        b_ = ell.b_;
    }
    return *this;
}

// Сравнение хар-ки полуосей
double Ellipse::getScale() const {
    return a_ * b_;
}



Elipse.h:

#ifndef ELLIPSE_H
#define ELLIPSE_H

#include "Function.h"

// Эллипс: x^2/a^2 + y^2/b^2 = 1
// Верхняя ветвь: y = b * sqrt(1 - x^2/a^2)
class Ellipse : public Function {
private:
    double a_;  // полуось по x
    double b_;  // полуось по y

public:
    // Конструктор по умолчанию
    Ellipse(); // Конструктор по умолчанию
    Ellipse(double a, double b); // Конструктор с параметрами
    Ellipse(const Ellipse& other); // Конструктор копирования

    ~Ellipse() override; // Деструктор

    double getA() const;
    double getB() const;

    // Виртуальные методы
    double compute(double x) const override;
    void print(std::ostream& os) const override;

    Ellipse& operator=(const Ellipse& other); // operator= для 1 типа
    Function& operator=(const Function& other) override; // operator= для разного типа

protected:
    double getScale() const override;
};

#endif