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

Overloading composed operators (+=,-=,*= and /=). Doesn't calculate how it should

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

#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();
}

输出:

*=操作后程序停止运行,所以它甚至不显示最后的结果。

运算符被错误地重载 他们创建了一个类型为 Fraction_ext 的新对象并更改它,同时运算符应更改左侧对象和 return 对已更改对象的引用。

例如

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