c++ - 重载组合运算符(+ =,-=,* =和/=)。不计算应如何

标签 c++ oop operator-overloading fractions compound-assignment

该代码是更复杂的代码的一部分,但是我减少了查看出现问题的部分。
所以在重载+ =,-=,* =和/ =运算符之后,结果都是一样的,我不知道自己在做什么错。
头文件如下所示:

#pragma once

#include<iostream>
using namespace std;

class Fraction
{
protected:
    int a, b;//protected attributes

public:
    Fraction() //explicit constructor w-out parameters
    {
        a = 0;
        b = 1;
    }

    Fraction(int aa, int bb) //explicit constructor with 2 parameter
    {

        if (bb != 0) //check before called
            a = aa;
        b = bb;
    }

    ~Fraction() //destructor
    {
        a = 0;
        b = 0;
    }

    void set_a(int denom = 0) //setters
    {
        a = denom;
    }
    void set_b(int nom = 1)
    {
        b = nom;
    }

    int get_a() //getters
    {
        return a;
    }
    int get_b()
    {
        return b;
    }
};


class Fraction_ext :public Fraction //inherited class
{
public:
    Fraction_ext(int x, int y) :Fraction(x, y) {}

    void simplify() // simplify() func and using the differences based algorithm
    {
        int c = abs(a);
        int d = abs(b);
        while (c != d)
        {
            if (c > d)
                c = c - d;
            else
                d = d - c;
        }
        a /= c;
        b /= c;
        cout << "\n\tFraction was simplified."; //displaying appropriate message
    }

    Fraction_ext operator+=(Fraction_ext F) //overloadng += using member method
    {
        Fraction_ext f(a, b);
        f.a += f.b;
        f.simplify();
        return f;
    }
    Fraction_ext operator-=(Fraction_ext F) //overloading -=
    {
        Fraction_ext f(a, b);
        f.a -= f.b;
        f.simplify();
        return f;
    }
    Fraction_ext operator*=(Fraction_ext F) //overloading *=
    {
        Fraction_ext f(a, b);
        f.a *= f.b;
        f.simplify(); //simplifying result after calc
        return f;
    }
    Fraction_ext operator/=(Fraction_ext F) //overloading /=
    {
        Fraction_ext f(a, b);
        f.a /= f.b;
        f.simplify();
        return f;
    }
};

源文件:
#include"Header.h"

int main()
{   
    int a, b, c, d;
    cout << "Enter attributes for ob1 and ob2:";
    cin >> a >> b; 
    cin >> c >> d;
    Fraction_ext ob1(a, b), ob2(c, d); //instantiate 2 objects for the inherited class

    //creating 4 other objects for the results
    Fraction_ext sum = ob1, dif = ob1, prod = ob1, div = ob1;
    ob1.simplify();
    ob2.simplify();

    cout << "\n\tOb1 simplified is: " << ob1.get_a() << "/" << ob1.get_b();
    cout << "\n\tOb2 simplified is: " << ob2.get_a() << "/" << ob2.get_b() << endl;
    sum += ob2;
    cout << "\nOverloaded += :" << sum.get_a() << "/" << sum.get_b();
    dif -= ob2;
    cout << "\nOverloaded -= :" << dif.get_a() << "/" << dif.get_b();
    prod *= ob2;
    cout << "\nOverloaded *= :" << prod.get_a() << "/" << prod.get_b();
    div /= ob2;
    cout << "\nOverloaded /= :" << div.get_a() << "/" << div.get_b();
}

输出:
enter image description here

* =操作后,程序停止工作,因此它甚至不显示最后的结果。

最佳答案

操作符被不正确地重载他们创建类型为Fraction_ext的新对象并对其进行更改,而运算符(operator)应更改左侧对象并返回对已更改对象的引用。

例如

Fraction_ext & operator +=( const Fraction_ext &F) //overloadng += using member method
{
    this->a += f.b;
    this->simplify();
    return *this;
}

我也怀疑这个说法
this->a += f.b;

加两个分数。因此,请检查算法。据我所知,两个分数的总和是这样计算的
a1   a2   a1 * b2 + a2 * b1 
-- + -- = -----------------
b1   b2        b1 * b2 

关于c++ - 重载组合运算符(+ =,-=,* =和/=)。不计算应如何,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61934988/

相关文章:

c++ - 编写调试器

类中的 C# 进度条?

javascript - 原型(prototype)和 document.getElementById()

c++ - 如果我只实现运算符 <,我可以使用运算符 == 吗?

c++ - Leet Code 正则表达式匹配问题

c++ - 在 Linux 上使用 ncurses 的背景颜色

c++ - 是否可以从 C++ 中此类中定义的结构内部调用类方法?

Python - 从项目根目录加载文件

vb.net - .NET 4 中是否允许泛型运算符重载?

原始类型的 C# 重载运算符