Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль
Повторная активизация e-mail

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 17.10.2020, 19:50   #1
Александр222
Пользователь
 
Регистрация: 15.04.2020
Сообщений: 59
По умолчанию Метод сложения в абстрактном классе

Как можно реализовать метод сложения? Есть абстрактный класс Pair, есть класс-наследник FazzyNumber и Fraction.
Fraction я реализовал, работает, а с FazzyNumber не получается
Как можно исправить?
Сама задача:
image_2020-10-15_141330 (1).jpg

Задание 1.31 из условия:
Безымянный.jpg

Вот код. Все, что связано с FazzyNumber - набросок.

Pair.h

Код:
#include <memory>
#include <iostream>
#include <functional>
 
class Pair {
public:
    int X;
    int Y;
    double x, e1, e2;
 
    void SetX(int);
    void SetY(int);
 
    int GetX() const;
    int GetY() const;
 
    Pair();
    Pair(int, int);
    Pair(const Pair& other);
 
    virtual void  Addition (const Pair& , Pair& ) = 0;
    virtual void Subtraction (const Pair& , Pair& ) = 0;
    virtual void Multiplication (const Pair& , Pair& ) = 0;
    virtual void Division (const Pair& , Pair& ) = 0;
 
    virtual bool Greater (Pair& ) = 0;
    virtual bool Equal (Pair& ) = 0;
 
    friend std::ostream& operator<<(std::ostream&, std::unique_ptr<Pair>&);
    friend std::istream& operator>>(std::istream&, Pair&);
};
 
class Fraction : public Pair {
public:
    Fraction();
    Fraction(int X, int Y);
    Fraction(const Fraction& other);
 
    void Addition(const  Pair&, Pair&);
    void Subtraction(const  Pair&, Pair&);
    void Multiplication(const  Pair&, Pair&);
    void Division(const  Pair&, Pair&);
 
    bool Greater(Pair&);
    bool Equal(Pair&);
 
    friend std::ostream& operator << (std::ostream& out, const Fraction& F);
};
 
class FazzyNumber: public Pair {
public:
    FazzyNumber();
    FazzyNumber(int X, int Y);
    FazzyNumber(const FazzyNumber& other);
   
    void Addition(const  Pair&, Pair&);
    void Subtraction(const  Pair&, Pair&);
    void Multiplication(const  Pair&, Pair&);
    void Division(const  Pair&, Pair&);
 
    bool Greater(Pair&);
    bool Equal(Pair&);
 
    friend std::ostream& operator << (std::ostream& out, const FazzyNumber& FN);    
    friend FazzyNumber operator + (const FazzyNumber& a, const FazzyNumber& b);
};
Pair.cpp

Код:
#include <iostream>
#include "Pair.h"
#include <cmath>
 
Pair::Pair() : X{0}, Y{0} {};
Pair::Pair(int X, int Y) : X {X}, Y {Y} {};
Pair::Pair(const Pair& other) : X {other.X}, Y {other.Y} {};
 
void Pair::SetX (int X) { X = X; }
void Pair::SetY(int Y) { Y = Y; }
 
int Pair::GetX() const { return X; }
int Pair::GetY() const { return Y; }
 
std::ostream& operator<<(std::ostream& out, std::unique_ptr<Pair>& p_num) {
    out << "[ " << p_num->GetX() << " : " << p_num->GetY() << " ]";
    return out;
}
 
std::istream& operator>>(std::istream& in, Pair& num) {
    in >> num.X >> num.Y;
    return in;
}
 
Fraction::Fraction() : Pair() {};
Fraction::Fraction(int X, int Y) : Pair(X, Y) {};
Fraction::Fraction(const Fraction& other) : Pair(other) {};
 
void Fraction::Addition(const  Pair& other, Pair& answer) {
    answer.X = (X * other.Y) + (other.X * Y);
    answer.Y = Y * other.Y;
}
 
void Fraction::Subtraction(const Pair& other, Pair& answer) {
    answer.X = (X * other.Y) - (other.X * Y);
    answer.Y = Y * other.Y;
}
 
void Fraction::Multiplication(const Pair& other, Pair& answer) {
    answer.X = X * other.X;
    answer.Y = Y * other.Y;
}
 
void Fraction::Division(const Pair& other, Pair& answer) {
    answer.X = X * other.Y;
    answer.Y = Y * other.X;
}
 
bool Fraction::Greater(Pair& value) {
    int Fraction1;
    int Fraction2;
    Fraction1 = (X / Y);
    Fraction2 = (value.X / value.Y);
    return Fraction1 > Fraction2;
}
 
bool Fraction::Equal(Pair& value) { return (X / Y) == (value.X / value.Y); }
 
std::ostream& operator<<(std::ostream& out, const Fraction& F) {
    out << F.X << ", " << F.Y;
    return out;
}
 
FazzyNumber::FazzyNumber() : Pair() {};
FazzyNumber::FazzyNumber(int X, int Y) : Pair(X, Y) {};
FazzyNumber::FazzyNumber(const FazzyNumber& other) : Pair(other) {}
 
void FazzyNumber::Addition(const Pair& X, Pair& Y){
     x = X.x + Y.x;
     e1 = X.x + Y.x - X.e1 - Y.e1;
     e2 = X.x + Y.x + X.e2 + Y.e2;
}
 
void FazzyNumber::Subtraction(const Pair& other, Pair& answer){
    answer.x = X - other.X;
    answer.e1 = X - other.X - e1 - other.e1;
    answer.e2 = X - other.X + e2 + other.e2;
}
 
void FazzyNumber::Multiplication(const Pair& X, Pair& Y){
    x = X.X * Y.X;
    e1 = X.X * Y.X - Y.X * X.e1 - X.X * Y.e1 + X.e1 * Y.e1;
    e2 = X.X * Y.X + Y.X * X.e2 + X.X * Y.e2 + X.e2 * Y.e2;
}
 
void FazzyNumber::Division(const Pair& X, Pair& Y){   
    if (Y.X <= 0) throw std::runtime_error("Division by 0");
    x = X.X / Y.X;      
   
   // if ((X.X - Y.X < 0.0001) && (Y.X + Y.e2 > -0.0001)) throw std::runtime_error("Division by 0");
    e1 = (X.X - Y.X) / (Y.X + Y.e2);
   
    //if ((X.X + X.e2) < 0.0001 && (Y.X - Y.e1) > -0.0001) throw std::runtime_error("Division by 0");
    e2 = (X.X + X.e2) / (Y.X - Y.e1);    
}
 
bool FazzyNumber::Greater(Pair &value){
    int FazzyNumber1;
    int FazzyNumber2;
    FazzyNumber1 = (X / Y);
    FazzyNumber2 = (value.X / value.Y);
    return FazzyNumber1 > FazzyNumber2;
}
 
bool FazzyNumber::Equal(Pair& value) { return (X / Y) == (value.X / value.Y); };
 
std::ostream& operator<<(std::ostream& out, const FazzyNumber& FN) {
    out << FN.X << ", " << FN.Y;
    return out;
}
 
FazzyNumber operator+(const FazzyNumber& a, const FazzyNumber& b)
{
    return FazzyNumber();
}
Александр222 вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц

Опции темы Поиск в этой теме
Поиск в этой теме:

Расширенный поиск


Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Помогите переделать код под виртуальный метод в классе Korol12 Общие вопросы C/C++ 4 13.05.2020 18:12
Реализовать в производном классе метод Adamson Общие вопросы C/C++ 6 08.12.2015 09:45
Метод в классе aleksskay Общие вопросы Delphi 2 21.09.2014 11:15
Метод перебора, Метод дихотомии, Метод золотого сечения Delphi !!! OneBri Помощь студентам 0 03.10.2012 08:42
Нужно ли в абстрактном классе реализовывать методы? psihadelic Общие вопросы C/C++ 1 22.04.2010 17:19